What is fragmentation?

Fragmentation refers to a memory utilization problem faced by operating system designers. It occurs when processes are loaded and unloaded to and from the main memory (RAM), leaving a chunk of free space in the main memory. The space cannot be utilized by other processes as it does not meet the minimum size requirement to load the process, leading to a waste of memory resources.

Types of fragmentation

There are two types of fragmentation:

Internal fragmentation

Internal fragmentation happens when a process requires memory that is less than the size of a memory block in the main memory. After the process is loaded into the main memory, it creates a little free space in the memory block. This chunk of memory cannot be used by any other process and leads to memory wastage.

Internal fragmentation in main memory
Internal fragmentation in main memory

In the example above, we can see that initially, we have 5 processes in the memory. Processes 1, 2, 4, and 5 use 1024 MBs, while process 3 uses 2048 MBs of space. After some time, process 2 and 5 finish their execution and are unloaded from the memory leaving free space for other processes to be loaded.

After a while, a new process, which is shown in red, requests a memory segment of 800 MBs. As there are free slots in the main memory, the process gets loaded without any issues. However, as the process only uses 800 MBs while the memory block has a fixed size of 1024 MBs, 224 MB of free space is still left in the allocated memory block. This free space cannot be allocated to any other process and ends up being wasted. This phenomenon is called internal fragmentation.

External fragmentation

External fragmentation can occur when a process arrives to be loaded into the main memory. However, the free space in the main memory is not contiguous. Hence, the program cannot be successfully loaded into the main memory which leads to a waste of memory resources.

External fragmentation in main memory
External fragmentation in main memory

Similarly, as we see in the example above, we again have five processes in the memory. Processes 1, 2, 4 & 5 use 1024 MBs while process 3 uses 2048 MBs of space. After a while, process 2 and 5 finish their execution and are unloaded from the memory, leaving free space for another process to be loaded.

A new process, shown in red, requests to be loaded into the main memory, which has 2048 MBs of free space available. Even though there is enough free space, the process cannot be loaded into the main memory as both memory blocks are non-contiguous. Therefore, the new process must wait till 2048 MBs of contiguous memory space are free before it can be run.

Issues with fragmentation

  • Memory wastage: With fragmentation, other processes cannot efficiently utilize free space in the main memory. This leads to the wastage of memory resources.

  • Slow speeds: When the main memory becomes excessively fragmented, the system may start to slow down as it cannot effectively assign memory to processes.

How to avoid fragmentation

As we know, internal fragmentation occurs due to processes that require memory that is less than the fixed size of the memory block. Some techniques to avoid internal fragmentation are as follows:

  • Buddy memory algorithm: Whenever a process requests to be loaded to the main memory, the algorithm finds the block that can be split and divides its address space by the power of 2. The process continues until we have the smallest chunk that could load the process.

Buddy memory algorithm
Buddy memory algorithm
  • Best fit memory allocation: We can check for all the available memory space and load the process onto the one with the shortest size which can hold the process.

Depiction of best fit memory allocation
Depiction of best fit memory allocation
  • Dynamic memory allocation: We can dynamically allocate memory, defining the size required so that the process does not use default variable sizes, resulting in better memory utilization.

Depiction of dynamic memory allocation
Depiction of dynamic memory allocation

External fragmentation occurs due to free space that is not contiguous. Below are some techniques that can be used to avoid external fragmentation:

  • Segmentation: We can split the memory required into variable-size parts and load them into the main memory individually.

Segmentation of logical address space
Segmentation of logical address space
  • Paging: Main memory is divided into fixed-sized blocks called frames, and a process is divided into fixed-sized blocks called pages. Pages are then loaded to the frames in the main memory in a more structured way. We do this to reduce external fragmentation, as a page occupies a whole frame and doesn't leave small gaps in the main memory.

Paging in main memory
Paging in main memory

Conclusion

Fragmentation is a huge memory management issue and affects the overall performance of systems drastically. Even though steps can be taken to reduce fragmentation, it cannot be eliminated. Regular memory defragmentation can be carried out by using tools such as Defraggler which rearranges memory blocks in a way that there are no free spaces in between processes.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved