Open In App

Difference Between Fragmentation and Compaction

Last Updated : 24 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In an operating system, memory management plays a vital role in maximum CPU utilization, when space is allocated to a process there is some loss of memory(fragmentation) leads to inefficient use of memory, and to reduce this loss one of the techniques (compaction) is used to optimize memory space.

Fragmentation

It occurs due to contiguous memory allocation, as there is a phenomenon in which storage space is not used at its max. Thus reducing the performance of the OS. There are two types of fragmentation: external fragmentation and internal fragmentation.

1. External: It occurs when the available space is not contiguous and the storage space is divided into a small number of holes. Both First fit and Best fit suffer from external fragmentation. 

Example problem: Size of different non contiguous small holes are 54 byte, 30 byte and 44 byte. Total non contiguous size is- 54+30+44=128 bytes. Process request is 120 bytes.

Solution: Could not proceed due to non contiguous holes leads to external fragmentation.

Example of External Fragmentation in the memory

Example of External Fragmentation in the memory

2. Internal: The memory left in a block after allocating space to a process leads to internal fragmentation. Paging suffers internal fragmentation. Example of Internal Fragmentation in memory

Example problem: Available block size 255 bytes. Process size 250 bytes.

Solution:The process is allocated to the block, creating a hole of 5 bytes which leads to internal fragmentation.

Example of Internal Fragmentation in memory

Example of Internal Fragmentation in memory

Compaction

It is a technique to move holes(small memory chunks) in one direction and occupied blocks to another side. A huge amount of time is wasted in performing compaction

Compaction Technique

Compaction Technique

Difference between Fragmentation and Compaction

S. No Fragmentation Compaction
1. It is due to the creation of holes (small chunks of memory space).  It is to manage the hole.
2. This creates a loss of memory. This reduces the loss of memory.
3. Sometimes, the process cannot be accommodated. Memory is optimized to accommodate the process.
4. External Fragmentation may cause major problems. It solves the issue of External Fragmentation.
5. It depends on the amount of memory storage and the average process size. It depends on the number of blocks occupied and the number of holes left.
6. It occurs in contiguous and non-contiguous allocation. It works only if the relocation is dynamic.

Fragmentation and compaction are two different processes related to memory management in computer systems.

Fragmentation occurs when there is not enough contiguous memory available to fulfill a memory allocation request. This can happen when memory is allocated and deallocated in a way that leaves small blocks of free memory scattered throughout the available memory space. Over time, this can result in a situation where there is enough free memory available, but it is not contiguous, making it difficult to allocate memory for larger requests.

Compaction, on the other hand, is a process where the memory manager rearranges the memory space to create larger blocks of contiguous memory. This is typically done by moving memory blocks around to eliminate small gaps between allocated blocks, and to move all the allocated memory to one end of the memory space. This can help to alleviate fragmentation and make it easier to allocate larger blocks of memory.

In summary, fragmentation is the condition that results when free memory is divided into small, scattered chunks, whereas compaction is a process of moving allocated memory around to create larger contiguous blocks of free memory. Fragmentation is generally considered to be a problem that needs to be avoided, whereas compaction is a technique that can be used to address fragmentation when it does occur.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads