The compaction is the technique with the help of which we can overcome the problem of fragmentation. Also, due to this reason the compaction is known as defragmentation.
External fragmentation occurs when total memory has enough space to allocate the process, but the available distributed memory blocks are so small that it is unable to allocate the process.
The goal of compaction is to shuffle the memory content such that all free memory comes together in one large block.
Definition – The process of putting the used partitions at one end and creating one big free area at the other end for the new process.
What is the need of compaction?
For example, a process P1 of size 100 Kb makes a request to the memory whose size is 1000 Kb, but the free spaces in memory are 20 Kb, 50 Kb, 80 Kb. Therefore, the memory has enough space (150 Kb) but it is not able to fulfil the memory request of the process P1 which is of the size (100Kb) and this is because the actual memory blocks are very small in size.
The process of compaction will bring all unused partitions or free partitions in one area and all used partitions in one area. This will help the process to satisfy their request of memory.
In our above example, this will bring 150 Kb on one end which will satisfy the memory size of the process P1 which is of 100 Kb.
Compaction method problems
Although, the compaction solves the problem of external fragmentation but there are some disadvantages of compaction.
- The compaction is only possible when the program supports dynamic reallocation. When the dynamic reallocation is supported by the program, the compaction process will reallocate the program and the data and change the base register to reflect the new base address. This is how the reallocation method takes place. So, if the program does not support dynamic reallocation then compaction cannot be applied.
- The compaction process is time consuming and very expensive. As the simplest algorithm of compaction requires to move all the process towards one end of the memory and all the free space towards the other direction to produce one large hole available memory. These all steps will first of all be costly and also this requires a lot of CPU time therefore it decreases the efficiency of the Computer system.
The compaction method is able to solve the external fragmentation but the disadvantages of compaction makes it a less effective solution. Now, the other ways to solve the external fragmentation is non contiguous memory allocation. Where the memory is divided into smaller memory chunks and distributed all over the main memory. The non contiguous memory allocation effectively deals with external fragmentation.
Frequently asked question (FAQ)
What is the purpose of using compaction in memory management?
The purpose of using compaction in memory management is to solve the problem of external fragmentation. External fragmentation does not allow the memory allocation to the process instead there is enough memory available.
What does compaction mean?
Compaction means memory reallocation in such a way that the free spaces of the memory are together in one large space and the used memory spaces are together in another direction.
What is the difference between compaction and fragmentation?
The difference between compaction and fragmentation is compaction is a method by which we solve the problem of external fragmentation and external fragmentation create the problem of insufficient memory.
Aayush Kumar Gupta is the founder and creator of ExploringBits, a website dedicated to providing useful content for people passionate about Engineering and Technology. Aayush has completed his Bachelor of Technology (Computer Science & Engineering) from 2018-2022. From July 2022, Aayush has been working as a full-time Devops Engineer.