Facebook Pixel

Memory Fragmentation

Imagine a busy parking lot. Cars are constantly arriving, parking, and eventually leaving. Over time, instead of having a solid block of empty spaces, you end up with random single empty spots scattered throughout the lot.

A large RV arrives, but because there are no three consecutive spots available, it cannot park—even though there are twenty single spots available in total. This is exactly what happens to RAM in an operating system. This phenomenon is called Fragmentation.

What is Fragmentation?

Fragmentation occurs when memory is allocated and deallocated frequently, leaving behind small blocks of free memory that are too tiny or scattered to be useful. As a result, memory is wasted, and new processes are blocked from executing.

There are two distinct types of fragmentation: Internal and External.

1. Internal Fragmentation

Internal Fragmentation happens when the operating system assigns a process a block of memory that is strictly larger than what the process actually requested. The leftover space INSIDE that allocated block is wasted. It cannot be used by the process, and it cannot be given to any other process because the block is locked.

text
Example of Internal Fragmentation:

OS Memory Policy: Fixed-size 100 KB partitions.
Process 1 Arrives: Requests 70 KB of memory.

Result:
The OS gives Process 1 a 100 KB block.
Process 1 uses 70 KB.
The remaining 30 KB is "Internal Fragmentation". It is trapped inside the block and completely wasted.

2. External Fragmentation

External Fragmentation happens when the total amount of free memory in the system is large enough to satisfy a process's request, but that memory is not contiguous (it is not in one solid block).

This usually happens in systems using Variable Partitioning. As processes of different sizes load and exit at different times, they leave behind irregularly sized "holes" scattered across the RAM.

text
Example of External Fragmentation:

System Free Memory: 150 KB total.
Layout: [Hole A: 50 KB] ... [Hole B: 50 KB] ... [Hole C: 50 KB]

Process 2 Arrives: Requests 100 KB of contiguous memory.

Result:
The OS denies the request. Even though 150 KB is free, there is no single 100 KB block available. The 150 KB is severely fragmented externally.

Internal vs External Fragmentation

FeatureInternal FragmentationExternal Fragmentation
Location of WasteInside the allocated block.Outside the allocated blocks (scattered holes).
Primary CauseFixed-size partitioning (giving a process more than it asked for).Variable-size partitioning (processes leaving irregularly sized holes).
Total Memory RuleTotal memory is irrelevant; space is wasted per block.Total free memory is sufficient, but not contiguous.

How to Reduce Fragmentation

Operating systems use different strategies depending on the type of fragmentation.

Fixing Internal Fragmentation

  • Variable Partitioning (Dynamic Allocation): Instead of fixed block sizes, only allocate the EXACT amount of memory a process requests.
  • Smaller Page Sizes: In paging systems, internal fragmentation still occurs on the very last page of a process. By keeping the standard page size small (e.g., 4 KB), the maximum wasted space per process is kept to a minimum.

Fixing External Fragmentation

  • Compaction (Defragmentation): The OS pauses all execution, copies the live processes, and shuffles them tightly together at one end of the RAM. This merges all the scattered free holes into one massive, usable block at the other end. While effective, Compaction requires dynamic relocation (Execution Time binding) and consumes a massive amount of CPU time.
  • Non-Contiguous Allocation (Paging & Segmentation): The modern solution. If processes are allowed to be broken up and scattered into whatever holes exist, external fragmentation ceases to be a problem. Paging is the primary method used by modern operating systems like Windows and Linux to permanently solve external fragmentation.

Summary

Fragmentation is the natural enemy of memory efficiency. Internal fragmentation wastes space quietly behind closed doors, while external fragmentation paralyzes the system by scattering resources too thinly. The evolution from contiguous block allocation to non-contiguous paging was driven almost entirely by the need to defeat fragmentation.

Sort the Concepts

Classify the following characteristics as belonging to either Internal or External Fragmentation.

Internal Fragmentation
External Fragmentation
Unsorted Items:
Typically caused by Fixed Partitioning.
Can be solved by shuffling memory via Compaction.
Wasted space exists INSIDE the allocated memory block.
Total free space is sufficient to run a process, but it is not contiguous.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.