Linux Internals

Memory Fragmentation in Linux: Causes, Symptoms and Fixes

By NileVPS Team · Published · 4 min read

اقرأ المقال بالعربي

Sometimes a server reports plenty of free memory, yet the kernel logs an allocation failure, or a long-running process keeps growing even though its real data stays the same size. Both are forms of memory fragmentation: free memory exists, but not in the shape that is needed. This article explains the two main kinds, how to detect them, and what you can realistically do on a VPS.

Two kinds of fragmentation

External fragmentation happens when free memory is split into many small, scattered pieces. The total is large, but there is no single contiguous block big enough for a request. In the Linux kernel this matters for allocations that need physically contiguous pages, such as huge pages, some network buffers and certain drivers.

Internal fragmentation happens inside an allocated block or inside an application's heap: memory is reserved but only partly used. A program that allocates and frees many objects of different sizes can end up holding far more memory than its live data needs, because its allocator cannot return partly used chunks to the operating system.

How the kernel allocates physical memory

Linux manages physical memory with the buddy allocator. Memory is grouped into blocks of 2^order pages: order 0 is one 4 KB page, order 1 is 8 KB, and so on up to order 10 (4 MB) on typical x86-64 systems. When a block is freed and its neighbor ("buddy") is also free, they merge into a larger block.

You can see the current state:

cat /proc/buddyinfo
Node 0, zone   Normal   1520   980   410   120    30     6     1     0     0     0     0

Each column is the number of free blocks of order 0, 1, 2 and so on. Many small blocks and zeros in the right-hand columns mean memory is fragmented: small requests succeed, large contiguous ones may not.

More detail by migration type:

sudo cat /proc/pagetypeinfo

The kernel groups pages as movable, reclaimable or unmovable to keep unmovable allocations from scattering everywhere, because only movable pages can be relocated later.

Symptoms to look for

  • dmesg messages such as page allocation failure: order:4 while free still shows memory available.
  • Transparent huge pages rarely being used even when enabled.
  • Latency spikes caused by the kernel doing direct compaction during an allocation.
  • An application whose resident memory (RSS) grows steadily while its workload is stable.

Useful checks:

dmesg -T | grep -i 'allocation failure'
grep -E 'thp_fault_alloc|thp_fault_fallback|compact_stall' /proc/vmstat
cat /sys/kernel/mm/transparent_hugepage/enabled

A high thp_fault_fallback relative to thp_fault_alloc means the kernel often failed to find a contiguous 2 MB block. A rising compact_stall counter means processes waited for compaction.

Kernel-side fixes

TechniqueHowWhen it helps
Memory compaction`echo 1 \sudo tee /proc/sys/vm/compact_memory`One-off defragmentation, for testing
Proactive compactionsysctl vm.compaction_proactiveness=20 (kernel 5.9+)Keeps memory less fragmented in the background
Keep a free reserveRaise vm.min_free_kbytes moderatelyGives atomic allocations more headroom
Adjust THPSet enabled or defrag to madviseAvoids stalls when apps do not need huge pages
RebootPlanned maintenanceResets fragmentation completely

Compaction moves movable pages to build larger free blocks; our article on memory compaction goes deeper. Change one setting at a time, measure, and persist values in /etc/sysctl.d/ only once they help. Avoid setting min_free_kbytes very high, since that memory becomes unavailable to applications and can trigger reclaim too early.

For transparent huge pages, madvise is a balanced choice on many servers: only applications that ask for huge pages get them, and others do not trigger compaction stalls:

echo madvise | sudo tee /sys/kernel/mm/transparent_hugepage/enabled
echo madvise | sudo tee /sys/kernel/mm/transparent_hugepage/defrag

Check your database's documentation first, because recommendations differ between products.

Application-side fragmentation

Fragmentation inside a process is often the bigger problem in practice:

  • Redis reports mem_fragmentation_ratio in INFO memory. A value well above 1 means Redis holds more memory than its data needs. Redis built with jemalloc supports activedefrag yes to reorganize memory online.
  • glibc malloc creates multiple arenas for multithreaded programs, which can inflate memory. Setting MALLOC_ARENA_MAX=2 for the service, or using jemalloc or mimalloc, often reduces RSS for long-running servers.
  • Garbage-collected runtimes (Java, Go, Node.js) have their own compaction and heap settings; tune those rather than the kernel.
  • Restarting workers periodically, for example PHP-FPM's pm.max_requests, caps the growth of fragmented heaps.

For a systemd service you can set an environment variable without editing the application:

sudo systemctl edit myapp
# add:
# [Service]
# Environment=MALLOC_ARENA_MAX=2
sudo systemctl restart myapp

Sizing considerations

Fragmentation is worse when memory runs close to full, because the kernel has little room to rearrange pages. Keeping healthy headroom, with available memory well above zero under peak load, reduces both allocation failures and compaction stalls. If your server constantly runs near its memory limit, a larger plan from our pricing page is often cheaper than hours of tuning.

FAQ

Is memory fragmentation a problem on every Linux server?

No. Most servers never notice it. It matters for long uptimes with heavy memory churn, workloads that need large contiguous allocations or huge pages, and long-running processes with uneven allocation patterns.

Does dropping caches fix fragmentation?

Not reliably. Dropping caches frees pages but does not arrange them into contiguous blocks, and it hurts performance afterward. Compaction is the kernel mechanism designed for this.

How can I tell kernel fragmentation from application fragmentation?

Kernel fragmentation shows up in /proc/buddyinfo, allocation failures in dmesg, and THP fallbacks. Application fragmentation shows up as one process's RSS growing while its data stays constant, or as a high ratio in tools like Redis INFO memory.

Related guides