Memory fragmentation is the condition in which free memory exists but cannot be used efficiently, because it is divided into small, scattered pieces or wasted inside allocated blocks. In memory management within operating systems, it is one of the main reasons a system can fail an allocation request even when the total amount of free memory looks sufficient.
Fragmentation appears at every level of the memory hierarchy: in physical page frames managed by the kernel, in kernel object caches, and in the heaps of individual programs. Its two classic forms, external and internal fragmentation, have different causes and different cures, and every allocator design is essentially a trade-off between them.
How memory becomes fragmented
Fragmentation arises from the pattern of allocations and frees over time. When blocks of different sizes are allocated and released in an unpredictable order, the free regions left behind lie between blocks that are still in use. The allocator can only hand out a request from a single contiguous region, so many small holes do not add up to one usable large block.
External fragmentation is free memory scattered outside allocated blocks, in gaps too small to be useful. Internal fragmentation is memory wasted inside allocated blocks, because the allocator rounded a request up to a fixed size, an alignment boundary or a whole page.
The table below separates the two forms and a third, closely related case seen in virtual memory systems.
| Form | Where the waste lies | Typical cause |
|---|---|---|
| External fragmentation | Gaps between allocated blocks | Variable-size allocations freed out of order |
| Internal fragmentation | Unused bytes inside a block | Rounding to size classes, pages or alignment |
| Physical page fragmentation | Scattered free page frames | Long-lived pages pinned across RAM |
| Address space fragmentation | Holes in a process’s virtual range | Many mappings created and removed |
Fragmentation in operating system allocators
Operating system kernels use specialized allocators that limit fragmentation for their specific workloads. Each one accepts some waste of one kind to reduce waste of the other, and the combination used in a kernel reflects years of tuning against real usage.
| Allocator | External fragmentation | Internal fragmentation |
|---|---|---|
| Paging with fixed frames | Eliminated for virtual memory | Up to one page per mapping |
| Buddy memory allocation | Reduced by merging buddies | Up to nearly 50% from power-of-two rounding |
| Slab allocation | Low within each cache | Small, limited to slab tail and alignment |
| Segmentation | High with variable segment sizes | Very low |
| General-purpose heap | Depends on fit policy | Depends on size classes |
Paging largely solves external fragmentation for virtual memory, because any free page frame can back any virtual page and contiguity in physical memory is not required. The problem returns whenever the kernel needs physically contiguous memory, for example for huge pages, for device buffers used by direct memory access without an IOMMU, or for large kernel allocations. Linux reports the state of its buddy allocator in the file /proc/buddyinfo, which shows how many free blocks remain at each order.
Measuring fragmentation
Fragmentation is measured by comparing usable free memory with total free memory. A common external fragmentation metric is one minus the ratio of the largest free block to the total free memory: a value near zero means free memory is mostly contiguous, while a value near one means it is shattered into small pieces.
| Indicator | What it shows | Typical tool or source |
|---|---|---|
| Largest free block | Biggest request that can still succeed | Allocator statistics |
| Free blocks per order | Availability of contiguous ranges | /proc/buddyinfo on Linux |
| Heap overhead ratio | Allocated bytes versus requested bytes | Heap profilers |
| Compaction activity | How often the kernel moves pages | Kernel memory counters |
| Huge page failures | Inability to form large pages | Transparent huge page statistics |
A high resident set size with low live data in a long-running program is a classic symptom of heap fragmentation. The process holds memory that it cannot return to the operating system, because a few live objects keep each partly used region in place.
Techniques that reduce fragmentation
Fragmentation is reduced by grouping similar allocations and moving data when possible. No single method removes it entirely, so practical systems combine several approaches at different layers.
- Memory compaction migrates movable pages to create large contiguous free regions, at the cost of copying and TLB shootdowns.
- Grouping by mobility keeps movable and unmovable pages in separate regions so that pinned pages do not block compaction.
- Size-class allocators place objects of similar size together, which limits external fragmentation in heaps.
- Arena or region allocation frees whole groups of objects at once, so short-lived data never leaves isolated holes.
- Reserving contiguous memory early, at boot, guarantees large buffers before fragmentation develops.
Garbage-collected runtimes add another option: a compacting collector moves live objects together and updates every reference to them. This removes external fragmentation in the managed heap, but it requires pausing or coordinating with the program and is not possible in languages that expose raw pointers.
Limitations and common misconceptions
Fragmentation is often misdiagnosed as a memory leak or as a simple shortage of RAM. The distinction matters, because the fixes are entirely different.
- Free memory is not available memory: a system can report gigabytes free and still fail an order-9 allocation.
- Fragmentation is not a leak: leaked memory is unreachable, whereas fragmented memory is reachable but unusable.
- More RAM only delays the problem in long-running systems with unfavorable allocation patterns.
- Defragmenting a disk is unrelated: file system fragmentation concerns storage layout, not main memory.
Compaction also has real costs. Moving pages consumes CPU time, invalidates cached translations and can introduce latency spikes, so latency-sensitive systems often limit or schedule it rather than run it on demand.
The idea in brief
Picture a parking lot where cars come and go all day. By evening there may be ten free spaces. But they are scattered one by one between parked cars. A bus needs several spaces in a row, so it still cannot park.
Computer memory works the same way. Programs take and return pieces of memory all the time. Free space ends up split into small gaps. The total looks large, but a big request may still fail. That waste is memory fragmentation.
A practical scenario
A database server with 256 GB of RAM has been running for three months. Monitoring shows about 40 GB free, yet the kernel increasingly fails to provide 2 MB huge pages for the database buffer pool, and query latency becomes uneven because memory is now backed by ordinary 4 KB pages with more TLB misses.
The administrator inspects /proc/buddyinfo and finds that almost all free memory sits in blocks of 4 KB to 64 KB, with very few larger blocks. The fix combines two steps: reserving huge pages at boot for the buffer pool and enabling proactive compaction for the remaining workload. After the next restart, huge page allocation succeeds consistently and latency returns to its earlier, stable range.