The word fragmentation is derived from the word “fragments” which means smaller pieces. Whenever a large chunk is divided into smaller pieces by any means or for any purpose, we call the process “fragmentation”. Fragmentation in computer systems, similarly, is a process of using main memory such that the storage space is used inefficiently and is divided into small portions of memory rather than a larger one piece of memory. We do this intentionally sometimes to use memory efficiently, for instance, in paging. But when the sum of all the smaller chunks of memory is larger than the needed memory but we cannot adjust the data in pieces of memory, this is called fragmentation. It reduces and efficiency as well as the performance of memory usage simultaneously.
Whenever we talk about fragmentation in the memory of a computer system, we mean small holes which are formed in memory house whenever a method is loaded or removed form a physical memory. Mostly in case of dynamic memory allocation, the process of memory allocation and deallocation results in memory fragmentation i.e. all the memory blocks are too small to fit in a given block of data however, the combined small blocks of memory are larger than the size of the data block to be saved in memory. In memory blocks, if there happens a fragmentation, the system fails to assign the contiguous memory house to processes. Fragmentation can be divided into 2 different types depending on the memory division method used in the memory.
Table of Contents
Types of fragmentation
Types of fragmentation includes,
- Internal fragmentation
- External fragmentation
For the sake of using memory efficiently, we divide memory in smaller fixed size blocks as we have studied in paging. The basic purpose of using pages is that these pages can be easily accessed and moved from secondary memory (hard disk) to main memory whenever we need processing. These blocks however sometimes result in the wastage of memory, as we will discuss in this article. Internal fragmentation happens once the memory is split into mounted sized blocks. Whenever CPU gets a process for execution the main memory stores the data of the process (code, heap and stack) in fixed sized blocks (pages) in the main memory. If the size of the block (fixed size) in the main memory is larger than the memory blocks a process can need than the left-over space which is neither too large to accommodate a complete process in it nor too small to be ignored when calculating memory wastage. This internal fragmentation when used occurs on a larger scale results in a large amount of memory wastage which cannot be ignored.
This leftover area within the mounted sized block can’t be allotted to any process because it wouldn’t be adequate to satisfy the request of memory by the method. However, additional memory is allotted generally than is required by the method, that eventually leads to excess memory going waste or left unused. The overhead of keeping track of the internals hole created because of internal fragmentation is considerably over the number of internal holes. The matter of internal fragmentation may be solved by partitioning the memory into the variable sized block and assign the best-sized block to a method requesting for the memory. Still, it’ll not entirely eliminate the matter of internal fragmentation however will cut back it to some extent.
Figure 1: Internal fragmentation
Solutions for internal fragmentation
The problem of internal fragmentation can be reduced by using variable sized memory blocks rather than fixed sized. These variable sized memory blocks can reduce internal fragmentation because whenever each memory item (data of process) arrives to the memory, memory creates a block of variable size in the memory and the size of pages are adjustable according to the need of the user or the process.
External fragmentation is way more similar to internal fragmentation, just a simple difference lies in between which I will discuss here. Let’s suppose there is one single chunk of memory available for storing information and data, and a process arrives, and we want to store it in the memory. The process will be stored in the memory dynamically in the memory, i.e. randomly, wherever it finds space or feels comfortable. When the next process arrives, it will adjust it accordingly in the memory space. But now let’s assume, we remove a process i.e. its data from the memory, in this case the process will leave a hole in the memory and it cannot be used with the other memory no matter even if it resides next to the free memory as shown in the figure below.
There is a total of 7kb available in the memory, but we cannot use it store a process of size 5 kb or larger because it is not contiguous, rather it is present in portions. This problem is called external fragmentation and it is also a result of inefficient memory usage which in the end results in memory wastage and performance issues.
Solutions for external fragmentation
One of the most commonly used solutions of external fragmentation is coalescing. In this method we actually combine all the free memory blocks and make it one large memory block as shown in the figure below,
We can also eliminate external fragmentation by using segmentation, as we have discussed in detail in previous articles. Also, we can reduce external fragmentation by using paging in memory but again paging will result in internal fragmentation, which further can be reduced by using variable sized pages rather than fixed sized pages. The figure below explains the external fragmentation and how it effects memory allocation in detail.
Figure 2: External fragmentation
What are the differences between internal and external fragmentation?
The following discussion below differentiates between internal fragmentation and external fragmentation.
- Internal fragmentation arsis due to the fixed sized pages and external was the result of dynamic memory allocation to processes.
- Internal fragmentation occurs within the pages and external fragmentation occurs between two pages.
- Intra pages fragmentation is known as internal and inter pages fragmentation is referred as external fragmentation.
Figure 3: Comparison between internal and external fragmentation
|Internal fragmentation||External fragmentation|
|Memory allocation||It occurs when fixed sized blocks of memory are assigned for used (paging)||It is caused mainly by dynamic allocation of memory with variable sized blocks.|
|Cause||It is caused when a larger memory block is assigned to a data/process of smaller size||It is caused when a process is removed from the memory.|
|Effects||In fixed sized blocks the leftover space in one block cannot be used for other processes||The removed process leaves a space or hole and the memory appears as a collection of small holes rather than a chunk.|
|Solution||It can be reduced by using variable sized blocks of memory and allocation memory using best fit approach||It can be avoided by using memory compaction or coalescing (joining two holes of memory).|