Understanding Linux Swap, Swappiness, and How to Monitor and Clear Swap Usage
This article explains what Linux swap is, its drawbacks, how to tune the swappiness parameter, methods for monitoring swap usage per system and per process, and practical ways to clear swap, including useful shell scripts and commands.
What is swap
Linux divides physical memory into pages; when an application cannot obtain enough physical memory, the kernel moves rarely used pages to a swap partition, freeing RAM for active processes.
Swap is important because it allows the system to allocate memory quickly when RAM is exhausted and to reclaim space from pages that are only needed during early initialization.
Drawbacks of swap
Disk I/O is orders of magnitude slower than RAM, even with SSDs, so heavy swapping degrades performance, especially for latency‑sensitive web servers where response time matters.
In short: keep a swap partition to avoid OOM kills, but avoid excessive swapping because it hurts responsiveness.
Manually tuning Linux swap – Swappiness
Swappiness (0‑100) controls how aggressively the kernel swaps out pages; higher values mean more aggressive swapping.
Typical settings: vm.swappiness = 0 → swap only to prevent OOM vm.swappiness = 60 → default vm.swappiness = 100 → aggressive swapping
Check the current value:
$ cat /proc/sys/vm/swappiness
60Change it temporarily:
$ sudo echo 0 > /proc/sys/vm/swappinessMake the change permanent by adding to /etc/sysctl.conf :
vm.swappiness = 0In the hotel‑search team the parameter is set to 0.
Monitoring server swap usage
Overall swap usage
$ free -m
total used free shared buffers cached
Mem: 7836 6078 1758 0 165 3000
-/+ buffers/cache: 2911 4924
Swap: 7999 0 7999Focus on the last line.
Swap in / swap out with vmstat
$ vmstat 1
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 0 1721436 170864 3090256 0 0 53 50 153 28 3 10 87 0 0
...Columns si and so represent swap‑in and swap‑out; the example shows no swapping activity.
Per‑process swap usage
Each process has a /proc/ /smaps file that lists memory mappings, including a Swap: field.
$ sudo cat /proc/25665/smaps | head -n9
40000000-40009000 r-xp 00000000 ca:07 1312980 /home/q/java/jdk1.6.0_37/bin/java
Size: 36 kB
Rss: 36 kB
Shared_Clean: 36 kB
Shared_Dirty: 0 kB
Private_Clean: 0 kB
Private_Dirty: 0 kB
Swap: 0 kB
Pss: 11 kBThe Swap field shows how much of that mapping resides in swap.
To aggregate swap usage across all processes, the following script can be used:
#!/bin/bash
function getswap {
SUM=0
OVERALL=0
for DIR in `find /proc/ -maxdepth 1 -type d | egrep "^/proc/[0-9]"` ; do
PID=`echo $DIR | cut -d / -f 3`
PROGNAME=`ps -p $PID -o comm --no-headers`
for SWAP in `grep Swap $DIR/smaps 2>/dev/null| awk '{ print $2 }'` ; do
let SUM=$SUM+$SWAP
done
echo "PID=$PID - Swap used: $SUM - ($PROGNAME )"
let OVERALL=$OVERALL+$SUM
SUM=0
done
echo "Overall swap used: $OVERALL"
}
getswap
#getswap|egrep -v "Swap used: 0"Running it on the author’s server yields:
$ getswap|egrep -v "Swap used: 0"
Overall swap used: 0For a quick check of Java processes only:
pgrep java | xargs -I{} sudo cat /proc/{}/smaps | grep 'Swap' | awk '{a+=$(NF-1)}END{print a}'How to clear occupied swap
Once you know which processes are using swap, you can either restart those services or run swapoff && swapon to move swapped pages back into RAM (provided enough free memory is available).
Example command:
sudo /sbin/swapoff -a && sudo /sbin/swapon -aAfter swapoff the free output shows swap size 0; after swapon the swap partition is re‑enabled.
References
http://www.data-recovery-app.com/datarecovery/linux-swap.html http://arminds.com/forums/linux-talk/226-configure-linux-swap-to-improve-server-performance http://en.wikipedia.org/wiki/Swappiness
Qunar Tech Salon
Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.
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.