Understanding Linux Swap: How Swappiness, kswapd, and Watermarks Control Memory Management
This article explains Linux swap fundamentals, detailing how the kernel uses swap partitions or files, the role of the swappiness parameter in balancing cache reclamation versus anonymous page swapping, the operation of kswapd, and the impact of memory watermarks on reclamation decisions.
What is swap and why does it exist?
Swap refers to a swap partition or file; you can list active swap areas with swapon -s:
[zorro@zorrozou-pc0 linux-4.4]$ swapon -s
Filename Type Size Used Priority
/dev/dm-4 partition 33554428 0 -1When physical memory is insufficient, the kernel moves some pages to swap to avoid OOM.
Memory pressure triggers swap usage, and the kernel’s memory reclamation is tightly coupled with swap. The article examines why reclamation occurs, which pages can be reclaimed, and when swap is performed.
Why perform memory reclamation?
Which memory can be reclaimed?
When does swapping occur during reclamation?
How does the actual swapping work?
Why perform memory reclamation?
The kernel needs free pages for sudden allocation requests and to keep page cache efficient, so it periodically reclaims memory. When a request exceeds free memory, a direct reclaim is triggered.
kswapd process periodically scans memory to keep free pages available.
Direct reclaim (directpagereclaim) occurs when allocation cannot be satisfied.
Both mechanisms ultimately call shrink_zone(), which invokes shrink_lruvec() to walk four LRU lists: anon inactive, anon active, file inactive, file active.
What does swappiness control?
The /proc/sys/vm/swappiness file (default 60, range 0‑100) defines how aggressively the kernel swaps pages. Higher values increase swap usage; lower values favor cache reclamation.
Swappiness influences the balance between swapping anonymous pages and cleaning file‑backed cache during reclamation. The kernel uses this value in get_scan_count() to decide the proportion of each.
At 100, anonymous and file pages are treated equally.
At 0, the kernel prefers cache cleaning, but will still swap if memory is critically low.
When does kswapd trigger swapping?
kswapd runs when free memory falls below the low watermark. It continues reclaiming until memory rises above the high watermark. Direct reclaim occurs when allocation cannot be satisfied.
What are memory watermarks?
Linux defines three watermarks per zone: high, low, and min, indicating memory pressure levels. When free memory drops below low, kswapd starts; below min, direct reclaim is invoked.
The calculation uses /proc/sys/vm/min_free_kbytes and zone sizes.
Related kernel parameters
zone_reclaim_mode– controls whether reclamation stays within the local zone (0‑4 values). min_unmapped_ratio – percentage of pages per NUMA node that must be free before reclaim. page-cluster – controls how many contiguous swap pages are prefetched (default 3, meaning 8 pages).
Swap manipulation commands
Create a swap file with mkswap, enable it with swapon, and disable with swapoff. Example commands are shown in the images.
Swap partition priority
When multiple swap areas exist, each has a priority (‑1 to 32767). Higher priority areas are used first. Equal priority leads to round‑robin usage, which can improve performance if swap devices are on different disks.
Applications can lock memory with mlock() to prevent swapping.
Conclusion
Swap configuration depends on workload. Larger swap can prevent OOM for memory‑intensive services, while disabling swap may avoid I/O stalls on bursty workloads. Adjust swappiness, watermarks, and related parameters according to your environment.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
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.
