Mastering Linux Swap: How Swappiness, kswapd, and Watermarks Control Memory
This article explains Linux swap fundamentals, the role of swappiness, kswapd behavior, memory watermarks, and related kernel parameters, providing practical guidance on configuring swap priority, zone reclaim, and other settings to optimize system performance under various workloads.
1. What is SWAP and what does it do?
Swap refers to a swap partition or file. On Linux you can view active swap spaces with
swapon -s:
<code>[zorro@zorrozou-pc0 linux-4.4]$ swapon -s
Filename Type Size Used Priority
/dev/dm-4 partition 33554428 0 -1</code>When memory is scarce, the kernel moves some memory pages to swap to avoid OOM conditions. Swap usage is tightly coupled with memory reclamation.
2. What does swappiness adjust?
The file
/proc/sys/vm/swappiness(default 60, range 0‑100) controls how aggressively the kernel swaps pages. Higher values increase swap aggressiveness; lower values favor cache reclamation. A value of 0 delays swapping until free and file‑backed pages drop below the high water mark.
Swappiness influences the kernel's preference between reclaiming anonymous pages (swap) and cleaning file‑backed cache during memory pressure.
3. When does kswapd perform swap operations?
kswapd runs periodic checks and triggers reclamation when memory watermarks indicate pressure (below
lowwatermark, stops at
high). Direct reclamation occurs when a memory allocation cannot be satisfied.
4. What are memory watermarks?
Linux defines three watermarks per zone: high (ample free memory), low (moderate pressure), and min (severe pressure). When free memory falls below
low, kswapd starts reclaiming; if it reaches
min, direct reclamation is invoked.
Watermarks are calculated from total memory and
/proc/sys/vm/min_free_kbytes.
5. Swap partition priority
When multiple swap devices are present, each has a priority (set with
swapon -p). Higher numbers are used first. Equal priorities cause round‑robin usage, which can improve I/O performance if swaps reside on different disks.
Related kernel parameters
zone_reclaim_mode : controls whether reclamation stays within the local NUMA node (0‑off, 1‑local, 2‑local with writeback, 4‑allow swap).
min_unmapped_ratio : NUMA‑specific percentage threshold for reclamation.
page-cluster : controls how many contiguous pages are prefetched from swap (power‑of‑two value).
Swap manipulation commands
Use
mkswapto create a swap area,
swaponto enable it, and
swapoffto disable it.
Conclusion
Whether to enable swap, how large it should be, and which parameters to tune depend on the specific workload. For memory‑intensive services like databases, a larger swap can prevent OOM kills; for latency‑sensitive workloads, minimizing swap avoids I/O stalls.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
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.