Fundamentals 9 min read

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.

Qunar Tech Salon
Qunar Tech Salon
Qunar Tech Salon
Understanding Linux Swap, Swappiness, and How to Monitor and Clear Swap Usage

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
60

Change it temporarily:

$ sudo echo 0 > /proc/sys/vm/swappiness

Make the change permanent by adding to /etc/sysctl.conf :

vm.swappiness = 0

In 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       7999

Focus 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 kB

The 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: 0

For 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 -a

After 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

Memory ManagementoperationsLinuxsystem monitoringshell scriptswapswappiness
Qunar Tech Salon
Written by

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.

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.