Understanding Linux Swap: How It Works, Tuning Tips, and Common Cleanup Commands
Linux swap provides a disk‑backed memory extension, storing anonymous pages when RAM runs low; this article explains swap’s operation, the kernel’s memory reclamation thresholds, tuning parameters like min_free_kbytes and swappiness, and offers commands to locate high‑swap processes and safely clear swap.
1. How Swap Works
In early computers memory was expensive and limited, so a disk area could be used as virtual memory called swap. Because disk I/O is much slower than RAM, reaching swap indicates memory pressure; high‑performance applications usually avoid swap by adding RAM.
Memory pages are classified as file pages (cached file data) and anonymous pages (heap, stack, etc.). Swap typically stores anonymous pages. Writing pages to swap is called “swap‑in” and reading them back “swap‑out”. If memory remains insufficient after reclamation, the OOM killer may terminate processes.
2. Memory Reclamation Mechanism
The kernel uses thresholds defined in /proc/sys/vm/min_free_kbytes and related values pages_low, pages_high, and pages_free. The daemon kswapd0 periodically scans memory and triggers reclamation based on where the free pages count falls relative to these thresholds.
If free memory < pages_min, only the kernel can allocate memory.
If free memory is between pages_min and pages_low, pressure is high; kswapd0 reclaims until free memory exceeds pages_high.
If free memory is between pages_low and pages_high, moderate pressure but new allocations can still be satisfied.
If free memory > pages_high, there is ample memory and no pressure.
3. Optimizing Swap Usage
Adjust /proc/sys/vm/min_free_kbytes to change the reclamation threshold.
Inspect /proc/zoneinfo to see memory distribution across zones (Normal, DMA, DMA32, etc.). Example output:
$ cat /proc/zoneinfo
...
Node 0, zone Normal
pages free 227894
min 14896
low 18620
high 22344
...
nr_free_pages 227894
nr_zone_inactive_anon 11082
nr_zone_active_anon 14024
nr_zone_inactive_file 539024
nr_zone_active_file 923986
...Set /proc/sys/vm/swappiness to control the preference between swapping anonymous versus file pages. A value of 0 favors RAM, while 100 aggressively uses swap. The default on Linux is 60, meaning swap starts being used when memory usage exceeds about 40% of total RAM.
Even with swappiness set to 0, the kernel may still use swap under extreme pressure.
Temporary change:
sysctl -w vm.swappiness=1 # or 0Permanent change (add to /etc/sysctl.conf and reload):
vm.swappiness = 0
sysctl -p4. Quickly Identifying High‑Swap Processes
Sort processes by their VmSwap value to list the PID, name, and swap usage:
for file in /proc/*/status; do
awk '/VmSwap|Name|^Pid/ {printf $2 " "}' $file
done | sort -k3 -n -r | headThis command outputs the processes that consume the most swap.
5. Common Swap Cleanup Methods
To clear swap, you can turn it off and on again:
swapoff -a && swapon -aNote that forcing a swap reset while the system is under memory pressure may cause instability, so use with caution.
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.
Tech Stroll Journey
The philosophy behind "Stroll": continuous learning, curiosity‑driven, and practice‑focused.
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.
