Types of Fragmentation in OS and How to Address Them
Fragmentation in an Operating System refers to the phenomenon where the available memory or storage space becomes divided into small, unusable pieces over time, leading to a decrease in system performance and efficiency. This can occur in both physical memory (RAM) and secondary storage (hard disk drives or SSDs).
Types
Internal Fragmentation
Occurs when the memory is allocated in fixed-size blocks, but the requested data is smaller than the block size. The remaining space within the block is wasted, leading to internal fragmentation.
External Fragmentation
Occurs when there is enough free memory to satisfy a request, but the memory is not contiguous. This leads to the creation of small unusable holes or fragments between memory blocks, leading to external fragmentation.
Possible Solutions
Defragmentation
Defragmentation is a process of reorganizing the files and folders on the hard disk or SSD so that they are contiguous. This process can help reduce external fragmentation and improve the performance of the system.
Compaction
Compaction is a process of shuffling the data in memory so that the free memory is gathered together into one large block. This process can help reduce internal fragmentation.
Dynamic Memory Allocation
In this method, the memory is allocated in small units as required rather than allocating fixed-size blocks. This helps to reduce both internal and external fragmentation.
Virtual Memory
Virtual Memory allows the computer to use a portion of the hard disk as an extension of RAM. This method can help reduce the effects of external fragmentation by allowing the system to allocate contiguous memory in the hard disk, even if the physical memory is fragmented.
Example
For example, let's consider the case of a hard disk with files stored in different locations. Over time, as new files are added and old files are deleted, the hard disk may become fragmented, leading to slower read and write speeds. In such cases, defragmentation software can be used to rearrange the files and folders on the hard disk so that they are contiguous, reducing external fragmentation and improving system performance. Similarly, in the case of dynamic memory allocation, the system can allocate memory as required in smaller units, reducing internal fragmentation and improving performance.
Subscribe to my newsletter
Read articles from Harsh Mange directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Harsh Mange
Harsh Mange
This is Harsh Mange, working as a Software Engineer - Backend at Argoid. I love building apps. Working on contributing to open-source projects and the dev community.