Difference Between Internal and External Fragmentation

Difference between internal and external fragmentation is a topic of interest to many who like to improve their computer knowledge. Before knowing this difference, we have to see what fragmentation is. Fragmentation is a phenomenon that occurs in computer memory such as Random Access Memory (RAM) or hard disks, which causes wastage and inefficient usage of free space. While the efficient usage of available space is hindered, this causes performance issues, as well. Internal fragmentation occurs when memory allocation is based on fixed-size partitions where after a small size application is assigned to a slot the remaining free space of that slot is wasted. External fragmentation occurs when memory is dynamically allocated where after loading and unloading of several slots here and there the free space is being distributed rather than being contiguous.

What is Internal Fragmentation?

Consider the figure above where a fixed sized memory allocation mechanism is being followed. Initially, the memory is empty and the allocator has divided the memory into fixed size partitions. Then later three programs named A, B, C have been loaded to the first three partitions while the 4th partition is still free. Program A matches the size of the partition, so there is no wastage in that partition, but Program B and Program C are smaller than the partition size. So in part ition 2 and partition 3 there is remaining free space. However, this free space is unusable as the memory allocator only assigns full partitions to programs but no t parts of it. This wastage of free space is called internal fragmentation.

In the above example, it is equal sized fixed partitions but this can even happen in a situation where partitions of various fixed sizes are available. Usually the memory or hardest space is divided into blocks that are usually the size of powers of 2 such as 2, 4, 8, 16 bytes. So a program or a file of 3 bytes will be assigned to a 4 byte block but one byte of that block will become unusable causing internal fragmentation.

What is External Fragmentation?

Consider the figure above where memory allocation is done dynamically. In dynamic memory allocation, the allocator allocates only the exact needed size for that program. First memory is completely free. Then the Programs A, B, C, D and E of different sizes are loaded one after the other and they are placed in memory contiguously in that order. Then later, Program A and Program C closes and they are unloaded from memory. Now there are three free space areas in the memory, but they are not adjacent. Now a large program called Program F is going to be loaded but neither of the free space block is not enough for Program F. The addition of all the free spaces is definitely enough for Program F, but due to the lack of adjacency that space is unusable for Program F. This is called External Fragmentation.

What is the difference between Internal and External Fragmentation?

• Internal Fragmentation occurs when a fixed size memory allocation technique is used. External fragmentation occurs when a dynamic memory allocation technique is used.

• Internal fragmentation occurs when a fixed size partition is assigned to a program/file with less size than the partition making the rest of the space in that partition unusable. External fragmentation is due to the lack of enough adjacent space after loading and unloading of programs or files for some time because then all free space is distributed here and there.

• External fragmentation can be mined by compaction where the assigned blocks are moved to one side, so that contiguous space is gained. However, this operation takes time and also certain critical assigned areas for example system services cannot be moved safely. We can observe this compaction step done on hard disks when running the disk defragmenter in Windows.

• External fragmentation can be prevented by mechanisms such as segmentation and paging. Here a logical contiguous virtual memory space is given while in reality the files/programs are splitted into parts and placed here and there.

• Internal fragmentation can be maimed by having partitions of several sizes and assigning a program based on the best fit. However, still internal fragmentation is not fully eliminated.

Summary:

Internal vs External Fragmentation

Both internal fragmentation and external fragmentation are phenomena where memory is wasted. Internal fragmentation occurs in fixed size memory allocation while external fragmentation occurs in dynamic memory allocation. When an allocated partition is occupied by a program that is lesser than the partition , remaining space goes wasted causing internal fragmentation. When enough adjacent space cannot be found after loading and unloading of programs, due to the fact that free space is distributed here and there, this causes external fragmentation. Fragmentation can occur in any memory device such as RAM, Hard disk and Flash drives.