Help Center/ Huawei Cloud EulerOS/ FAQs/ How Does the System Reclaim Memory?
Updated on 2025-10-09 GMT+08:00

How Does the System Reclaim Memory?

Linux Memory Reclamation Triggers

  1. 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.
  2. 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.
  3. 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

  1. 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.
  2. 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()).
  3. 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.
  4. Trigger an OOM kill.
    • If page caches have been reclaimed and the swap space is full, Linux triggers the OOM killer.