Facebook Pixel

Compaction in Operating Systems

Over time, as processes load into memory and finish their work, the free space they leave behind gets scattered all over the place. Individual gaps here and there might each be too small to be useful, even when their combined size would be more than enough to load a new process.

Compaction is the heavy-duty housekeeping operation the operating system uses to clean up this mess.

What is Compaction?

Compaction is the physical shifting of active processes. The OS slides all the currently running processes tightly toward one end of memory. This naturally pushes all the scattered free gaps toward the other end, forcing them to merge into one massive, usable block of free space.

However, this does not happen constantly. Compaction is an extremely expensive operation. The OS only triggers it when external memory fragmentation has built up to a critical point where it is genuinely blocking new processes from running.

Memory Compaction Diagram

Compaction: Shifting active processes to one side of RAM to merge all free space into a single block.

How Compaction Works

Consider a scenario where three processes are loaded, but they are separated by small holes of 15MB, 10MB, and 8MB. A new process arrives needing 30MB of contiguous space. It gets rejected, even though 33MB is technically free.

text
Before Compaction:
| P1 (20MB) | FREE (15MB) | P3 (25MB) | FREE (10MB) | P4 (20MB) | FREE (8MB) |

The OS pauses execution and physically shifts P3 and P4 to the left so they sit right up against P1, closing all the gaps.

After Compaction:
| P1 (20MB) | P3 (25MB) | P4 (20MB) | FREE (33MB) |

Now that the single 33MB free block has been formed, it easily fits the new 30MB process. The total free memory never changed; it was just arranged more usefully.

Dynamic Relocation: The Hidden Requirement

Moving a process in memory sounds simple, but it creates a massive technical problem. A process generates memory addresses based on where it was originally loaded. If the OS suddenly slides it to a different location, all its internal pointers and addresses instantly become wrong. It would crash immediately.

To prevent this, Compaction absolutely requires Dynamic Relocation (Execution Time Binding).

text
Physical Address = Base Address (Relocation Register) + Logical Address

With dynamic relocation, the OS uses a hardware Relocation Register to store the base address of wherever the process currently lives. The process itself only knows its logical addresses (e.g., Address 10).

If a process originally sat at address 5000 and the OS moves it to address 12000 during compaction, the OS simply updates the Relocation Register from 5000 to 12000. When the process resumes and asks for Address 10, the hardware does the math (12000 + 10) on the fly. The process never even knows it was moved.

Advantages vs Drawbacks

Advantages of CompactionDrawbacks of Compaction
Eliminates Scattered Free Blocks: All little pockets of free space are consolidated into one contiguous region.Heavy CPU Cost: Physically copying process data byte-by-byte across memory takes immense processing power.
Better Memory Utilization: Clears up external fragmentation, ensuring a higher percentage of RAM is actually usable.Processes Get Interrupted: Running processes must be completely paused while they are being moved, causing noticeable disruption.
Opens the Door for Large Processes: Processes that were blocked indefinitely in a fragmented queue can finally load and execute.Strict Hardware Requirement: Compaction is mathematically impossible without hardware support for Dynamic Relocation.

Summary

Compaction is a targeted, brute-force fix for external fragmentation. By physically rearranging processes so that free memory consolidates into one place, the OS recovers space that would otherwise sit wasted.

However, because it requires dynamic relocation support and comes with severe costs in CPU time and process interruption, it is used very selectively. It is a necessary evil in contiguous memory systems, but its high overhead is exactly what pushed modern operating systems to adopt Paging instead.

Dynamic Relocation Math

Question 1 of 1

Test your understanding of how dynamic relocation allows compaction to work seamlessly.

A process generates a logical request for Address 50. It was originally loaded at physical address 3000. During a compaction event, the OS shifts the entire process forward in RAM to start at physical address 8000. What happens next?
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.