Advantages of Fragmentation
Failures: Without fragmentation, if we have no sequential free space, then the write operation will fail. Faster data writes: Faster data writes due to short fragments. Optimization of memory Storage: Fragmentation focus is to use every part of memory without wasting the memory.Disadvantages of Fragmentation
Slower read times: Due to fragmentation, the amount of time it takes to read a non-sequential file can grow and can lead to slow read. Need for regular defragmentation. Sometimes we need to defragment the memory. There are two types of fragmentation;- External fragmentation
- Internal fragmentation
External Fragmentation
Total free RAM space is enough to load a process but the process still can’t load because free blocks of RAM are not contiguous. In other words, we can say that all free blocks are not located together. For example, in the following diagram, we have a total 10 KB space free but it is not together located, so if a process with 10KB size wants to loads on the RAM, then can’t load due to external fragmentation. But in the diagram on the right side we can see that 10KB space is contiguously free, so here if a process of 10KB wants to load on the RAM then can execute.
Internal Fragmentation
Internal fragmentation occurs when fixed sized memory blocks are available and a process gets a block that is too much larger than the storage requirement of a process. The worst fit is mostly a big reason for internal fragmentation.
Difference between internal and external fragmentation in OS in tabular form
External Fragmentation | Internal Fragmentation | |
Basics of fragmentation | When different size memory blocks are dynamically allocated to the processes. | When fixed sized memory blocks are allocated to the processes. |
when fragmentation occurs | When the process releases the RAM, it creates the free space in the RAM which leads to cause external fragmentation. | When the memory assigned to the process is greater as compared to the memory requested by the process. |
solution of fragmentation | Segmentation, paging and Compaction | The memory must be partitioned into variable sized blocks. Further, assign the best-fit block to the process. |
First-fit, Best fit, and Miss fit memory allocation

First-fit memory allocation
In first-fit memory allocation, OS Allocates the first hole to the process that is big enough and the page size is less.Best-fit memory allocation
In best-fit memory allocation, OS Allocate the most suitable smallest hole to the page. In best-fit memory, memory waste is less as compared to the worst fit and first fit memory allocations.Worst-fit memory allocation
A frame is allocated to the page that is more than the requirements of the page, and the best-fit frame is available.Case Study of memory Fragmentation in operating systems.
Suppose we have some memory frames of 16KB, 14KB, 4KB, 20KB, 18KB, 7KB, 9KB, 12KB, and 15K. Simmilalry we, have pages of 12KB, 10KB, and 9KB. How you will allocate the memory if you follow first fit, best fit, worst fit memory allocation scheme. Example of First Fit Memory allocation 12KB page will reside in a memory frame of 16KB. 10KB page will reside in a memory frame of 14KB. 9KB page will reside in a memory frame of 20KB. Example of Best Fit Memory allocation 12KB page will reside in a memory frame of 12KB. 10KB page will reside in a memory frame of 14KB. 9KB page will reside in a memory frame of 9. Example of worst Fit Memory allocation 12KB page will reside in a memory frame of 20KB. 10KB page will reside in a memory frame of 18KB. 9KB page will reside in a memory frame of 15KB. Example of Next Fit Memory allocation 12KB page will reside in a memory frame of 16KB. 10KB page will reside in a memory frame of 14KB. 9KB page will reside in a memory frame of 20KB.Worst-fit memory allocation
In the Worst-fit memory allocation, OS Allocates the largest hole to the process and results in the wastage of a large amount of memory. Fragmentation is a memory management techniques We can divide the memory management techniques into two main parts;- Uni-programming
- Multi-programming
- contiguous memory allocation
- noncontiguous memory allocation
Contiguous memory allocation
contiguous memory means that all the free blocks of memory are closely neighboured with each other.Contiguous memory allocation in C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include <stdio.h> #include <stdlib.h> int main() { // This pointer will hold the base address of the block int* T4Tutorials_Pointer; int n, i; // Total elements for the array n = 5; printf("Please Enter number of elements: %d\n", n); // calloc() is used to Dynamically allocate the memory T4Tutorials_Pointer = (int*)calloc(n, sizeof(int)); // memory successfully allocated or not if (T4Tutorials_Pointer == NULL) { printf("Memory not allocated.\n"); exit(0); } else { // Confirmation of Memory allocated printf("Memory is successfully allocated with the help of calloc funtion.\n"); // Getting the elements of the array for (i = 0; i < n; ++i) { T4Tutorials_Pointer[i] = i + 1; } // Displaying the elements of the array printf("The elements of the array are: "); for (i = 0; i < n; ++i) { printf("%d, ", T4Tutorials_Pointer[i]); } } return 0; } |
Noncontiguous memory allocation
Non-contiguous memory allocation means that all the free blocks of memory are not closely neighboured with each other. How can we prevent memory fragmentation? Let’s suppose that we are able to isolate exactly those places where we are likely to allocate the big blocks of the memory. In case of Windows, we can do it directly by calling the VirtualAlloc function and we can do it without the memory manager. In this way, we can avoid fragmentation within the normal memory manager. VirtualAlloc function Memory can be allocated by the VirtualAlloc function. The virtualAlloc function is automatically initialized to zero. If we want to allocate memory in the address space of another process, then using the VirtualAllocEx function is the best option. Syntax of VirtualAlloc function
1 2 3 4 5 6 |
LPVOID VirtualAlloc( LPVOID lpAddress, SIZE_T dwSize, DWORD flAllocationType, DWORD flProtect ); |