In this lesson, you will learn how to manually trigger kernel-level memory compaction to resolve fragmentation and recover large contiguous memory blocks.
The Problem: External Fragmentation
As your system runs, the kernel allocates and frees memory of varying sizes. Over time, physical RAM becomes "swiss-chested"—riddled with small, non-contiguous holes. Even if you have plenty of total free memory, the kernel may fail to allocate a large, physically contiguous block (like a transparent hugepage or a large device buffer) because those free fragments are scattered across the address space. This is external fragmentation.
Memory Compaction
Linux handles this automatically via the background kcompactd thread, which attempts to move movable pages to consolidate free space. However, on high-performance systems or during periods of extreme memory pressure, you may need to force this process to ensure high-order memory allocations succeed without triggering the heavy-handed OOM Killer or latency-inducing direct reclaim.
Forcing Compaction via Sysctl
You can manually instruct the kernel to perform memory compaction by interacting with the /proc/sys/vm/compact_memory interface. Writing a 1 to this file tells the kernel to scan all memory zones and compact them immediately.
# Verify current fragmentation index if needed, then trigger compaction
echo 1 > /proc/sys/vm/compact_memory
This operation is synchronous and can be CPU-intensive, as the kernel must identify movable pages, relocate them, and update the corresponding page tables. You should only perform this when you observe a failure to allocate high-order pages (often reported in dmesg as "page allocation failure").
Monitoring Compaction
To see if your efforts are yielding results, you can monitor the compact-related counters in /proc/vmstat. Look for compact_migrate_success and compact_migrate_failure. If the success count climbs after running the command, you are successfully consolidating physical memory pages and improving the kernel's ability to satisfy future large-scale allocation requests.
While the kernel manages this autonomously, manual intervention provides a "defragmentation" mechanism that prevents high-latency spikes during critical workloads by ensuring large memory segments are ready and available for immediate use.
Key Takeaway: Triggering compact_memory forces the kernel to relocate movable pages into contiguous physical blocks, resolving external fragmentation and preventing allocation failures for high-order memory requests.