Contiguous Allocation
Compaction Needed
Read aloud is unavailable in this browser.
Defragmenting the Disk
When external fragmentation renders contiguous allocation unusable, compaction (also called defragmentation) is the remedy. Compaction consolidates free space by relocating files to eliminate gaps, creating larger contiguous regions.
However, compaction is expensive—both in time and I/O resources. Understanding when and how to compact is essential for systems that rely on contiguous allocation.
By the end of this page, you'll understand the compaction process, various compaction algorithms, the substantial costs involved, and strategies for minimizing compaction overhead.
The Compaction Process
Compaction works by moving files to consolidate free space. The basic algorithm:
- Identify holes — Locate all free regions on the disk
- Select files to move — Choose files adjacent to holes
- Relocate files — Read file data, write to new location
- Update metadata — Modify directory entries with new locations
- Mark old space free — Add vacated blocks to free list
- Repeat — Continue until free space is consolidated
Compaction Algorithms
Different compaction strategies trade off simplicity, speed, and effectiveness:
- •Simple Compaction: Move all files to one end of the disk. Maximum consolidation but most data movement.
- •Partial Compaction: Merge only adjacent holes by moving the smallest neighboring file. Less I/O but incomplete consolidation.
- •Selective Compaction: Move only files that create the biggest consolidated regions. Optimizes benefit per I/O cost.
- •Background Compaction: Compact gradually during idle time. Minimizes user-visible impact.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
/* * Simple Compaction Algorithm * * Moves all files toward block 0, leaving all * free space at the end of the disk. */ typedef struct { char name[256]; uint64_t start_block; uint32_t block_count;} file_entry_t; void compact_disk(disk_t *disk, file_entry_t *files, int count) { uint64_t next_free = 0; /* Next available position */ /* Sort files by current start position */ sort_by_start_position(files, count); for (int i = 0; i < count; i++) { file_entry_t *f = &files[i]; if (f->start_block > next_free) { /* Need to move this file */ printf("Moving %s: block %lu -> %lu", f->name, f->start_block, next_free); /* Read from old location */ void *buffer = malloc(f->block_count * BLOCK_SIZE); read_blocks(disk, f->start_block, f->block_count, buffer); /* Write to new location */ write_blocks(disk, next_free, f->block_count, buffer); /* Update directory entry */ f->start_block = next_free; update_directory(f); free(buffer); } next_free = f->start_block + f->block_count; } /* All space from next_free onward is now free */ mark_range_free(disk, next_free, disk->total_blocks - next_free);}The Costs of Compaction
Compaction is expensive. Let's quantify the costs:
| Cost Type | Measurement | Impact |
|---|---|---|
| Data Movement | ~500 GB read + 500 GB write | Hours of I/O |
| Time (HDD) | ~6-8 hours at 100 MB/s | Long outage |
| Time (SSD) | ~30-60 minutes at 500 MB/s | Significant delay |
| Disk Wear (SSD) | 500 GB of writes | Reduced lifespan |
| Availability | System unusable or degraded | User impact |
| Power Consumption | Sustained high I/O | Resource cost |
Compaction can take longer than the time saved by faster sequential access. For frequently modified file systems, the cost of repeated compaction outweighs the benefits of contiguous allocation entirely.
Online vs Offline Compaction
Offline Compaction:
- System is unavailable during compaction
- Simpler implementation (no concurrency concerns)
- Faster completion (no interruptions)
- Used in early systems and maintenance windows
Online Compaction:
- System remains available during compaction
- Must handle concurrent file operations
- Complex locking and consistency requirements
- May take longer but preserves availability
Challenges of Online Compaction:
- •Atomicity: File moves must be atomic—partial moves corrupt data
- •Locking: Files being moved must be locked against modification
- •Directory Updates: Must be synchronized with file relocation
- •Open Files: Cannot move files with active file handles (easily)
- •Crash Recovery: Must handle failures mid-compaction
Strategies to Minimize Compaction
Given compaction's costs, systems try to minimize its necessity:
- •Over-allocation: Allocate extra space for files that may grow, reducing future fragmentation.
- •Delayed Allocation: Don't allocate until data is actually written, allowing better placement decisions.
- •Best-Fit Allocation: Minimize wasted space per allocation, though creates tiny fragments.
- •Preventive Moves: Relocate small files during idle time before fragmentation becomes severe.
- •Hybrid Allocation: Use contiguous for read-heavy files, linked/indexed for others.
Modern file systems largely avoid the compaction problem by using extent-based allocation—files get contiguous extents when possible, but can span multiple non-contiguous extents when needed. This provides most of the sequential access benefits without the fragmentation crisis.
Summary: Compaction
- •Compaction consolidates free space by relocating files to eliminate fragmentation gaps.
- •Costs are substantial — hours of I/O, system unavailability, SSD wear.
- •Online compaction preserves availability but adds complexity.
- •Prevention strategies can reduce but not eliminate fragmentation.
- •Modern systems use hybrid approaches to avoid pure contiguous allocation's limitations.
You now understand compaction—the necessary but expensive remedy for external fragmentation. Next, we'll examine the scenarios where contiguous allocation remains the best choice despite these challenges.
KEEP LEARNING
Keep learning for free.
Read the complete lesson, save your notes and pick up where you left off with a free account. No card needed.