Operating System - Fragmentation



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. In simpler words, fragmentation means there are some gaps in the memory after running multiple processes at random memory locations.

Fragmentation can lead to wastage of memory. Even though there is enough total memory available, it may not be possible to allocate memory to a new process due to fragmentation. The image below shows fragmentation happening in main memory −

Fragmentation in Memory

This is an example of external fragmentation. Here the process P1 is allocated three frames of memory (F1, F2, and F3). P2 is allocated two frames of memory (F5 and F6). Similarly, P3 is allocated F8 frame.

Now, if a new process P4 requires three frames of memory, it cannot be allocated memory even though there are three free frames (F4, F7, and F9) available in the memory. This is because these free frames are not contiguous.

Types of Fragmentation

Fragmentation can be classified into two types −

Internal Fragmentation

Internal fragmentation occurs when a process gets more memory than it actually needed. The operating system allocates memory in fixed size blocks or frames. If a process uses only a part of the allocated memory, then some memory will be wasted. This can happen for large number of processes running in the system, which will lead to wastage of large amount of memory.

The following image shows internal fragmentation taking place in main memory −

Internal Fragmentation

In the above image, the process P1 is allocated F1, F2, and F3 frames of memory. But it is only using F1 and F2 frames. The F3 frame is wasted because the process does not need it. Similarly, the F6, F7, and F9 frames are also wasted because the processes P2 and P3 are holding them.

Causes of Internal Fragmentation

Internal fragmentation can occur due to the following reasons −

  • Fixed Size Memory Allocation − Some operating systems allocate memory in fixed size blocks or frames. If a process requires less memory than the allocated block size, then the remaining memory will be wasted. For example, consider a system that allocates memory to all the processes in blocks of 4 KB. If a process requires 6 KB of memory, then OS should allocate 8 KB of memory (2 blocks) to it. So 2 KB of memory will be wasted.
  • Management Overheads − Some systems reserve a part of the allocated memory for purpose like bookkeeping and management. Sometimes this reserved memory may not be used by the process.
  • Memory Alignment − Some systems require memory to be aligned to certain boundaries (like 4-byte or 8-byte boundaries). This can lead to wastage of memory and internal fragmentation.

External Fragmentation

External fragmentation refers to gaps generated in memory when multiple processes are loaded and unloaded from memory. To understand this, suppose that a process P1 is allocated with 3 frames of memory (F1, F2, and F3). P2 allocated with 1 frame F4 and P3 allocated with 2 frames F5 and F6. Now, if P2 is terminated, then the frame F4 will be free. But this introduced a gap in memory between the memory allocated to P1 and P3.

The following image shows how external fragmentnation takes place −

External Fragmentation

The gaps generated in memory will add up over time as more processes are loaded and unloaded from memory. This can lead to a situation where even though there is enough total memory available, the system will not be able to allocate memory to a new process. Because the available memory is scattered in small non-contiguous blocks.

Causes of External Fragmentation

External fragmentation can occur due to the following reasons −

  • Dynamic Memory Allocation − Some systems use dynamic memory allocation. In such systems, memory is allocated to processes when they request it and deallocated when they are done. This can cause fragmentation as there is no rule to allocate memory.
  • Process Termination − When a process is completed, it will be terminated from memory. This will create gaps in memory, if the terminated process was allocated memory in between other processes.
  • Variable Size Memory Allocation − Some systems allocate memory to processes in variable sizes. This can lead to fragmentation as there is no rule to allocate memory. For example, if a process requires 5 KB of memory, then OS should allocate 8 KB of memory (2 blocks of 4 KB) to it. So 3 KB of memory will be wasted.

How to Reduce Fragmentation?

There are several techniques to reduce fragmentation in memory. Some of the common techniques are:

  • Compaction − Compaction is a technique where the operating system rearranges the memory contents to place all free memory together in one large block. This will remove the gaps in memory and reduce fragmentation.
  • PagingPaging is a memory management technique that uses virtual memory to store the process and then maps it to frames of physical memory. This will eliminate external fragmentation as the process can be stored in non-contiguous frames of memory.
  • Segmentation − Segmentation divides memory into variable sized segments based on the logical structure of a program. This will reduce fragmentation as the segments can be allocated in non-contiguous memory locations.

Conclusion

Fragmentation is a common problem in memory management. It lead to wastage of memory even though there is enough total memory available. We have two types of fragmentation: internal fragmentation and external fragmentation. Internal fragmentation occurs when a process gets more memory than it actually needed. External fragmentation occurs when there are gaps in memory due to loading and unloading of processes. The techniques like compaction, paging, and segmentation can be used to reduce fragmentation in memory.

Advertisements