How the Linux Page Reclaim and OOM Killer Work: LRU Lists, Watermarks, and Why the System Kills Processes
A process that calls malloc for a multi-gigabyte buffer under memory pressure does not always receive the pages. The kernel may already be scanning lists of existing pages, writing dirty ones out, or preparing to kill something. That work is the reclaim subsystem. It is distinct from the moment a page fault installs a frame and from the buddy allocator that hands out free blocks.
This article explains the contract: zone watermarks that decide when reclaim starts, the LRU lists that order victims, how kswapd and direct reclaim differ, what is actually reclaimed (clean file pages, dirty file pages, anonymous pages), and why the OOM killer exists when reclaim cannot produce enough free memory. It is not a second tour of page-table walks, SLUB freelists, or the page-cache radix tree. Related reading: How the Linux Buddy and SLUB Allocators Work, How Virtual Memory Works, and How the Linux Page Cache Works.
The contract
Every zone (DMA, DMA32, Normal, and sometimes others) tracks free pages and three watermarks: min, low, and high. Above high the zone is comfortable. Between low and high the kernel wakes the background reclaim thread kswapd. Below min ordinary allocations are not supposed to proceed unless the caller is allowed to use reserves or can sleep and enter direct reclaim. GFP flags encode those permissions. GFP_KERNEL may reclaim and sleep. GFP_ATOMIC and GFP_NOWAIT may not. The same flags appear in the buddy allocator article; here they decide whether the caller itself becomes a reclaimer.
Reclaim does not free arbitrary memory. It targets pages that the kernel believes can be discarded or written back without violating correctness: clean file-backed pages (simply drop and reread later), dirty file-backed pages (write back first), and anonymous pages (swap out if swap is available). Kernel pages, locked pages, and certain pinned mappings are skipped or handled specially.
LRU lists and aging
Linux maintains per-zone LRU lists that approximate recency. Historically these were active and inactive lists for file and anonymous pages; modern kernels use a more refined multi-generational scheme with working-set protection. A page that is accessed moves toward the active end. A page that is not accessed for long enough is moved toward the inactive end and becomes a reclaim candidate.
The scanner walks the inactive lists, looking for pages it can free. For a clean file page the page-cache entry can be dropped. For a dirty file page the writeback path is invoked. For an anonymous page the swap subsystem is asked to allocate a swap slot and write the contents. Referenced bits (or their software equivalents) protect recently used pages so that a single scan does not empty the working set. The goal is to free cold pages while leaving hot ones resident.
kswapd versus direct reclaim
kswapd is a per-node kernel thread that runs in the background when a zone drops below the low watermark. It can sleep, perform writeback, and continue until the zone returns above the high watermark or until it has done a bounded amount of work. Because it runs asynchronously, most allocations never notice it.
Direct reclaim occurs inside the allocation path of a process that is allowed to sleep (typically GFP_KERNEL). The caller itself becomes the reclaimer for a short time: it scans LRUs, may wait for writeback, and only then retries the allocation. This is why an innocent malloc or a page fault can suddenly take milliseconds or longer under pressure. GFP_ATOMIC callers skip this path and fail immediately if free memory is insufficient.
Both paths share the same core scanning and isolation logic. The difference is latency and who pays it. kswapd tries to keep the machine ahead of demand. Direct reclaim is the synchronous fallback when that effort was not enough.
What actually gets reclaimed
Clean file pages are the cheapest. The kernel drops the page-cache mapping; the next read will fault the page back from storage (or from a still-cached copy if another mapping remains). Dirty file pages require writeback; the reclaim path coordinates with the writeback threads so that pages are not freed until they are clean. Anonymous pages require swap. If swap is disabled or full, they cannot be reclaimed this way. Overcommit policy (vm.overcommit_memory) affects whether the kernel allowed the original allocation in the first place; it does not change the later reclaim rules.
The page cache, dentry cache, and inode cache are also reclaimable. Under pressure the kernel shrinks these caches so that the memory can be returned to the free lists. A workload that touches millions of unique paths can therefore see the first lookup after reclaim become expensive again; that cost appears in the VFS and page-cache articles as the consequence of this subsystem.
Why the OOM killer exists
Reclaim can fail. Swap may be full, dirty pages may be stuck behind a slow device, locked pages may dominate, or the allocation may be for a high-order contiguous block that compaction cannot assemble. When an allocation that is allowed to use reserves still cannot obtain memory, and reclaim has already been tried, the kernel invokes the OOM killer.
The OOM killer selects a victim process according to a badness score. The score prefers processes that use a large amount of memory, have a low nice value (or are otherwise not protected), and are not essential system processes. The chosen process receives SIGKILL. Its pages are then freed, giving the original allocation a chance to succeed. The killer is not a fairness mechanism and not a scheduler; it is a last-resort recovery action that accepts data loss in one process to keep the rest of the system alive.
Containers and cgroups change the picture. The memory controller can enforce a limit and trigger OOM inside the cgroup before the host is exhausted. The victim is then chosen among the processes inside that cgroup. Host-level OOM still exists for kernel allocations and for processes outside any limiting cgroup.
Misconceptions
- Reclaim is the same as garbage collection. User-space GC finds unreachable objects. Kernel reclaim finds pages the kernel is allowed to discard or swap according to their mapping type and reference state. Reachability is not the criterion.
- The OOM killer runs only when RAM is completely full. It runs when a specific allocation cannot be satisfied after reclaim, which can happen with free pages still present if those pages are the wrong order, the wrong migrate type, or locked.
- kswapd eliminates latency. It reduces the probability of direct reclaim. Under a sudden spike or when writeback is slow, processes still enter direct reclaim and can stall.
- Disabling swap disables reclaim. Clean file pages and caches can still be reclaimed. Only anonymous pages lose their reclaim path.
Takeaways
Zone watermarks decide when reclaim starts. LRU aging decides which pages are candidates. kswapd performs the work in the background; direct reclaim performs it synchronously inside a sleeping allocation. Clean file pages are dropped, dirty file pages are written back, and anonymous pages are swapped if possible. When those steps cannot free enough memory for the current allocation, the OOM killer selects a process by badness and kills it so the system can continue. The mechanisms sit above the buddy allocator and below the page-fault handler; understanding them explains why malloc or a fault can suddenly become slow, and why a process can disappear with an OOM message even when top still shows some free memory.