Understanding Linux Kernel Page Reclamation Mechanisms
This article explains how Linux manages memory through page reclamation, detailing the role of LRU algorithms, page classification, reverse mapping, direct and asynchronous reclaim paths, and the OOM killer, while providing code examples and practical tuning advice.
Linux memory management relies on page reclamation to free unused pages and maintain system stability. The kernel classifies pages as anonymous or file-mapped, uses page descriptors, and employs reverse mapping to track virtual-to-physical relationships.
LRU (Least Recently Used) lists organize pages into active and inactive queues, allowing the kernel to select the least recently accessed pages for reclamation. The second‑chance algorithm refines this selection by checking the access bit before eviction.
When memory pressure rises, the kswapd daemon periodically scans zones and triggers asynchronous reclamation. In critical situations, direct reclamation is performed synchronously, invoking functions such as __alloc_pages_direct_reclaim and try_to_free_pages to free pages immediately.
Core reclamation functions walk through zones, nodes, and memory cgroups, shrinking LRU lists via shrink_inactive_list and shrink_page_list . These routines handle page isolation, unmapping, writeback, and freeing, while updating statistics and handling special cases like huge pages and swap cache.
Special scenarios include periodic checks by kswapd, direct reclamation during large allocation failures, suspend‑to‑disk (sleep) reclamation, and the OOM killer, which terminates processes based on oom_score when memory cannot be reclaimed.
Practical tuning involves adjusting the vm.swappiness parameter to control swap aggressiveness, monitoring CPU load and swap activity with tools like top and vmstat , and optimizing application memory usage to reduce reclamation overhead.
Deepin Linux
Research areas: Windows & Linux platforms, C/C++ backend development, embedded systems and Linux kernel, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.