Updated on 2025-10-09 GMT+08:00
How Does the System Reclaim Memory?
Linux Memory Reclamation Triggers
- kswapd reclamation (background, asynchronous)
- The kernel daemon kswapd continuously checks the following memory watermarks:
- high: The memory is normal and reclamation is not required.
- low: kswapd is triggered to reclaim some pages.
- min: The memory is still insufficient even if kswapd has been triggered for reclamation.
- This is gentle memory reclamation that is executed in the background and does not block processes.
- The kernel daemon kswapd continuously checks the following memory watermarks:
- Direct Reclaim (foreground, synchronous)
- When a process requests memory but the system does not have enough memory available, the process directly reclaims memory by itself.
- The reclamation is synchronous and may block processes.
- Out of Memory (OOM)
- If the free physical memory is still insufficient for applications after Direct Reclaim is executed, the kernel triggers OOM to kill processes or enter a panic state.
Linux Memory Reclamation Policy
- Reclaim page caches (file pages) first.
- For a clean page, discard it directly. (lowest cost)
- For a dirty page, write it back to the disk and then discard it.
- Reclaim slab caches (kernel objects such as dentry and inode).
- Most of the kernel's object caches (memory managed by the slab or slub allocator), such as dentry caches and inode caches, can be reclaimed.
- The kernel releases some of them using a shrinker (for example, shrink_slab()).
- Swap anonymous pages (heaps/stacks).
- Anonymous pages are created by processes for heap allocations, stack memory, and malloc calls. Anonymous pages are not backed by files on a disk. To reclaim them, you must write them to the swap partition. The kernel writes inactive anonymous pages to the swap partition and then releases the physical pages.
- The cost is much higher than that of reclaiming caches.
- Trigger an OOM kill.
- If page caches have been reclaimed and the swap space is full, Linux triggers the OOM killer.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
The system is busy. Please try again later.
For any further questions, feel free to contact us through the chatbot.
Chatbot