Fundamentals 52 min read

How Linux Memory Reclamation Works: Zones, Swap, and Compression Explained

This article explains Linux's memory reclamation mechanisms, covering the role of memory as the system's bloodstream, the three reclamation paths (fast, direct, kswapd), zone watermarks, page cache structures, reverse mapping, and how swap and compression are used to keep the system stable under memory pressure.

Deepin Linux
Deepin Linux
Deepin Linux
How Linux Memory Reclamation Works: Zones, Swap, and Compression Explained

In the vast Linux system, memory acts like the bloodstream, continuously delivering data and instructions to processes and ensuring stable operation.

1. Memory Pressure Crises

When many processes run or a single process consumes large memory, the system experiences memory pressure, leading to noticeable slowdown as data is swapped to disk (Swap). Severe pressure can trigger the OOM (Out‑Of‑Memory) killer, which terminates high‑memory processes and may cause a system crash.

2. When Does Memory Reclamation Occur?

Memory reclamation for zones can be divided into three types: fast reclamation, direct reclamation, and kswapd reclamation.

Fast Reclamation : Triggered in get_page_from_freelist() when a zone's free pages after allocation would fall below a watermark (min/low/high). The reclamation path is get_page_from_freelist() → zone_reclaim() → __zone_reclaim() → shrink_zone() . It checks zone_reclaim_mode and may limit reclamation to nearby zones. <code>struct scan_control sc = { .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), .gfp_mask = memalloc_noio_flags(gfp_mask), .order = order, .priority = ZONE_RECLAIM_PRIORITY, .may_writepage = !!(zone_reclaim_mode &amp; RECLAIM_WRITE), .may_unmap = !!(zone_reclaim_mode &amp; RECLAIM_SWAP), .may_swap = 1, }; </code> Reclamation repeats up to four shrink_zone() calls until enough pages are freed or priority reaches zero. It never writes dirty file pages even if zone_reclaim_mode allows writeback. Start condition: free pages after allocation < watermark + reserved pages. End condition: required pages reclaimed or priority reaches zero. Objects reclaimed: clean file pages, slab caches, possibly anonymous pages.

Direct Reclamation : Invoked during slow allocation when all zones fail the min watermark. The flow is __alloc_pages_slowpath() → __alloc_pages_direct_reclaim() → __perform_reclaim() → try_to_free_pages() → do_try_to_free_pages() → shrink_zones() → shrink_zone() . It scans all zones with default priority 12, performing up to 12 shrink_zone() passes. <code>struct scan_control sc = { .nr_to_reclaim = SWAP_CLUSTER_MAX, .gfp_mask = memalloc_noio_flags(gfp_mask), .order = order, .nodemask = nodemask, .priority = DEF_PRIORITY, .may_writepage = !laptop_mode, .may_unmap = 1, .may_swap = 1, }; </code> Direct reclamation never writes dirty file pages; it may write back non‑file pages if allowed. Start condition: no zone can satisfy the min watermark. End condition: 32 pages reclaimed, priority reaches zero, or enough free pages for compression. Objects reclaimed: processes exceeding their memcg soft limit, clean file pages, slab, anonymous swap pages.

kswapd Reclamation : Triggered when get_page_from_freelist() cannot allocate with the low watermark and the allocation flag does not contain __GFP_NO_KSWAPD . Each node has a kswapd thread that runs the following path: balance_pgdat() → kswapd_shrink_zone() → shrink_zone() . <code>struct scan_control sc = { .gfp_mask = GFP_KERNEL, .order = order, .priority = DEF_PRIORITY, .may_writepage = !laptop_mode, .may_unmap = 1, .may_swap = 1, }; </code> kswapd aims to raise each zone's free pages above the high watermark. Start condition: all zones fail the min watermark, waking all kswapd threads. End condition: every zone’s free pages > high watermark + reserved pages. Objects reclaimed: processes over memcg soft limit, clean and dirty file pages, slab, anonymous swap pages.

3. Linux Memory Reclamation Mechanism

2.1 Reclaimed Objects: Anonymous and File Pages

Anonymous pages (heap, stack, mmap) are swapped to a swap partition when memory is tight. File pages are cached data; clean pages can be freed directly, while dirty pages must be written back before being released.

2.2 Zones as Reclamation Units

Memory is divided into zones (DMA, Normal, HighMem). Each zone has three watermarks: WMARK_MIN , WMARK_LOW , and WMARK_HIGH . Allocation uses the low watermark for fast allocation; if that fails, the min watermark is used for slow allocation, and severe pressure triggers direct or fast reclamation.

2.3 Zone Watermarks

MIN : Used for slow allocation; failure leads to direct reclamation.

LOW : Default for fast allocation; falling below triggers fast reclamation.

HIGH : Desired free page count; kswapd tries to raise zones above this level.

4. Memory Reclamation Methods

4.1 LRU Lists

The kernel maintains LRU (Least Recently Used) lists for pages. Non‑active anonymous and file pages are placed at the tail; reclamation scans from the tail toward the head, freeing pages that are not actively referenced.

4.2 Page Cache Structure

Each file has an address_space that holds a radix tree (now an xarray) of cached pages. The structure includes:

<code>struct address_space {
    struct inode *host;
    struct radix_tree_root page_tree;
    spinlock_t tree_lock;
    struct prio_tree_root i_mmap;
    spinlock_t i_mmap_lock;
    unsigned long nrpages;
    struct address_space_operations *a_ops;
    ...
};
</code>

Pages are indexed by their offset within the file, allowing fast lookup via find_get_page() . Tags in the radix tree indicate page state (clean, dirty, writeback).

4.3 Reverse Mapping

When a page is reclaimed, the kernel must clear all PTEs that reference it. Reverse mapping computes the virtual address of a page within a VMA: <code>static inline unsigned long __vma_address(struct page *page, struct vm_area_struct *vma) { pgoff_t pgoff = page_to_pgoff(page); return vma->vm_start + ((pgoff - vma->vm_pgoff) &lt;&lt; PAGE_SHIFT); } </code> This allows the kernel to locate and invalidate all mappings before freeing the page. 4.4 Memory Allocation Process All allocations funnel through __alloc_pages_nodemask() , which first attempts fast allocation using the low watermark, then slow allocation with the min watermark, invoking asynchronous compression, direct reclamation, and finally OOM if needed. The allocation respects NUMA preferences via preferred_zone and zonelist . 4.5 Scan Control Structure <code>struct scan_control { unsigned long nr_to_reclaim; gfp_t gfp_mask; int order; nodemask_t *nodemask; struct mem_cgroup *target_mem_cgroup; int priority; unsigned int may_writepage:1; unsigned int may_unmap:1; unsigned int may_swap:1; unsigned int hibernation_mode:1; unsigned int compaction_ready:1; unsigned long nr_scanned; unsigned long nr_reclaimed; }; </code> Key fields control whether pages can be written back, unmapped, or swapped, and set the scanning priority. 4.6 Kernel Parameters Influencing Reclamation /proc/sys/vm/zone_reclaim_mode : Enables zone reclamation (bits 0‑write, 2‑unmap). /proc/sys/vm/laptop_mode : Affects direct reclamation writeback behavior. /proc/sys/vm/swappiness : Balances scanning of anonymous vs. file LRU lists (0‑only file, 200‑only anonymous). 5. Advanced Reclamation Techniques 5.1 Page Reclamation and LRU Algorithm The kernel uses a doubly‑linked LRU list; recently accessed pages move to the head, while the tail holds the least‑recently‑used pages that are reclaimed first. 5.2 Page Swapping: Memory‑Disk Interaction When memory is scarce, inactive pages are moved to the swap partition (Swap Out). If later needed, they are read back (Swap In). Excessive swapping increases disk I/O and can degrade performance. 5.3 Memory Compression Linux can compress inactive pages in RAM using zRAM, reducing the need for swap I/O. Compressed pages are stored in a special RAM‑backed block device and decompressed on demand. 5.4 Anonymous Page Discard In extreme pressure, the kernel may discard anonymous pages whose contents can be regenerated, freeing memory without swapping. This is done cautiously to avoid process crashes.

Memory ManagementkernelLinuxpage cacheswapzRAMMemory Reclamation
Deepin Linux
Written by

Deepin Linux

Research areas: Windows & Linux platforms, C/C++ backend development, embedded systems and Linux kernel, etc.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.