Open In App
Related Articles

Compaction in Operating System

Improve Article
Save Article
Like Article

Compaction is a technique to collect all the free memory present in the form of fragments into one large chunk of free memory, which can be used to run other processes.

It does that by moving all the processes towards one end of the memory and all the available free space towards the other end of the memory so that it becomes contiguous.

It is not always easy to do compaction. Compaction can be done only when the relocation is dynamic and done at execution time. Compaction can not be done when relocation is static and is performed at load time or assembly time.

Before Compaction

Before compaction, the main memory has some free space between occupied space. This condition is known as external fragmentation. Due to less free space between occupied spaces, large processes cannot be loaded into them.

Main Memory
Occupied space
Free space
Occupied space
Occupied space
Free space

After Compaction

After compaction, all the occupied space has been moved up and the free space at the bottom. This makes the space contiguous and removes external fragmentation. Processes with large memory requirements can be now loaded into the main memory.

Main Memory
Occupied space
Occupied space
Occupied space
Free space
Free space

Purpose of Compaction in Operating System

While allocating memory to process, the operating system often faces a problem when there’s a sufficient amount of free space within the memory to satisfy the memory demand of a process. however the process’s memory request can’t be fulfilled because the free memory available is in a non-contiguous manner, this problem is referred to as external fragmentation. To solve such kinds of problems compaction technique is used.

Issues with Compaction

Although the compaction technique is very useful in making memory utilization efficient and reduces external fragmentation of memory, the problem with it is that a large amount of time is wasted in the process and during that time the CPU sits idle hence reducing the efficiency of the system.

Advantages of Compaction

  • Reduces external fragmentation.
  • Make memory usage efficient.
  • Memory becomes contiguous.
  • Since memory becomes contiguous more processes can be loaded to memory, thereby increasing scalability of OS.
  • Fragmentation of file system can be temporarily removed by compaction.
  • Improves memory utilization as their is less gap between memory blocks.

Disadvantages of Compaction

  • System efficiency reduces and latency is increased.
  • A huge amount of time is wasted in performing compaction.
  • CPU sits idle for a long time.
  • Not always easy to perform compaction.
  • It may cause deadlocks since it disturbs the memeory allocation process.

FAQs on Compaction

1. Is compaction used in real time operating system?

Yes, it can be but needs to avois time voilations.

2. Does compaction occupy additional disk space?

It acquires temporary storage space.

3. What are some alternatives to memory compaction for avoiding external fragmentation?

fixed-size allocation, memory pools, dynamic allocation.

4. Can a user initiate memory compaction manually?

Some systems provide this feature to optimize memory usage.

Level Up Your GATE Prep!
Embark on a transformative journey towards GATE success by choosing Data Science & AI as your second paper choice with our specialized course. If you find yourself lost in the vast landscape of the GATE syllabus, our program is the compass you need.

Last Updated : 21 Sep, 2023
Like Article
Save Article
Similar Reads
Complete Tutorials