Memory Compaction
You'll learn how to reorganize processes in memory to eliminate fragmentation and create larger, usable free blocks.
The Fragmentation Problem
From the previous lesson, you know that contiguous memory allocation can create fragmentation—scattered small holes of free space that are individually too small to satisfy allocation requests, even though their total size might be sufficient.
Memory compaction is the solution: physically move processes around in memory to consolidate all free space into one large contiguous block.
How It Works
Imagine a bookshelf where books (processes) are scattered with small gaps between them. Memory compaction is like sliding all the books to one end, leaving all the empty space grouped together on the other end.
The operating system:
- Pauses running processes
- Copies the memory contents of each process to new locations
- Updates each process's base register to reflect its new location
- Adjusts all internal pointers within the relocated memory
- Resumes the processes
After compaction, all allocated memory sits together at one end, and all free memory forms a single large block at the other end.
The Analogy
Think of a parking lot with cars (processes) scattered throughout, leaving many single empty spots (fragments) but no room for a bus (large process). Compaction means asking everyone to move their cars together, creating a large empty area where the bus can park.
The Cost
Compaction is expensive:
- Copying large amounts of memory data takes time
- All affected processes must be frozen during the move
- CPU resources are consumed updating addresses
That's why compaction is typically used only when fragmentation becomes severe or when simpler solutions (like choosing different placement strategies) fail.
Key Takeaway: Memory compaction eliminates fragmentation by relocating processes to create one contiguous free block, but at the cost of significant CPU time and temporary process suspension.