Operating System - Compaction



Compaction is a technique used by operating systems to reduce fragmentation in memory. Fragmentation in a memory is a condition where the available memory is divided into small and non-contiguous blocks after allocating and deallocating memory to various processes. Read this chapter to understand how compaction works, its advantages, and drawbacks.

What is Compaction?

Compaction refer to the process of rearranging free memory space to together into a single contiguous block. This is done by moving the allocated memory blocks to one end of the memory and combining all the free memory blocks into a single block. The main aim of compaction is to reduce fragmentation of memory, and allocate memory to large processes more easily.

Compaction is performed when the operating system detects that there is a large amount of fragmentation in memory, which can occur once in a week or month depending on the system usage. Compaction can be a time and resource consuming process, as it requires moving processes in memory. Therefore, it is not performed frequently.

How Compaction Works?

The image below shows an example of how compaction works in main memory −

Compaction in Memory

In the image above,

  • Initially, there are three processes P1, P2, and P3 allocated memory contiguously.
  • When P2 is completed it's execution, it is removed from memory. This creates a gap in memory between P1 and P3.
  • Now, compaction is performed to rearrange the memory. P3 is moved to the end of P1, and all the free memory blocks are combined into a single block.

Dynamic Relocation For Compaction

The compaction can only be performed if the operating system supports dynamic relocation. Dynamic relocation refers to the ability of the operating system to move processes in memory while they are running. This is done by using a relocation register, which holds the base address of the process in memory. When a process is moved to new location, the base address in the relocation register is updated accordingly.

The logical address generated by the CPU is added to the base address in the relocation register to get the physical address in memory. i.e., the formula used is −

$$\mathrm{( \text{Logical Address} = \text{Base Address} + \text{Physical Address} )}$$

Where,
Base Address = Value in Relocation Register
Physical Address = Address generated by the CPU

If dynamic relocation is not supported, then compaction cannot be performed. In such cases, the operating system may have to use other techniques to reduce fragmentation, such as paging or segmentation.

Advantages of Compaction

The advantages of compaction are −

  • Reduces Fragmentation − The main aim of compaction is to reduce fragmentation in memory. This is achieved by rearranging the memory and combining all the free memory blocks into a single block.
  • Improves Memory Utilization − When fragmentation is reduced, it improves the overall memory utilization and efficiency of the system.
  • Allocation for Large Processes − Compaction makes it easier to allocate memory to large processes. Since all the free memory blocks are combined into a single block, it is easier to find a contiguous block of memory large enough to accommodate the process.

Drawbacks of Compaction

The drawbacks of compaction are −

  • CPU Overhead − Compaction requires extra effort and CPU time to move processes in memory. This can lead to increased CPU overhead and reduced system performance.
  • Process Interruption − Compaction requires interrupting the running processes to move them in memory.
  • Not Always Possible − Compaction can only be performed if the operating system supports dynamic relocation.

Conclusion

Compaction is a technique where free memory blocks are rearranged to form a single contiguous block in memory. This helps to reduce fragmentation of memory and improves efficiency of memory utilization. To perform compaction, the operating system must support dynamic relocation, which allows moving processes in memory while they are running. The main drawbacks of compaction can be noted as increased CPU overhead and Interruption of running processes. But overall, compaction is an effective technique to manage memory in operating systems.

Advertisements