Loading content...
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.
Compaction works by moving files to consolidate free space. The basic algorithm:
Different compaction strategies trade off simplicity, speed, and effectiveness:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
/* * 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\n", 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);}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.
Offline Compaction:
Online Compaction:
Challenges of Online Compaction:
Given compaction's costs, systems try to minimize its necessity:
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.
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.