This is an introductory article on the topic of swap with some applications on Linux operating system.
The generic term for all operating systems is Memory paging and it basically means the use of disk as a temporary solution when the main memory is not enough (obviously with some performance penalty). In Unix and derived systems, like Linux, the term swap is used.
We use a monitoring system that, among other things, tracks how much swap is used on physical or virtual machines. We recently decided not to monitor swap on virtual machines anymore, for several reasons:
Memory and swap status can be shown with the command free
but the dedicated command for swap is swapon
, used to activate or show swap status. There is a counterpart swapoff
used to disable swap. To see the swap status, the flag -s
(summary) is used:
# swapon -s
Filename Type Size Used Priority
/dev/dm-0 partition 8388604 0 -2
In the above example you see a dedicated partition (/dev/dm-0
) is used as swap. In Linux you may also setup a simple file to be used as swap.
There are countless of webpages describing this in detail so I'll give here only a brief version:
# run as root or prefix all commands with sudo
dd if=/dev/zero of=/swapfile bs=1M count=1024 # create 1024MB empty file
# named /swapfile
mkswap /swapfile # format the file for swap usage
chmod 600 /swapfile # set restrictive permissions
swapon /swapfile # activate swap
# edit /etc/fstab to make it permanent
This is a partial screenshot of the top
command on Linux:
I marked in red two areas of interest:
As a side note, you should know very clearly everything written on the %CPU line in the top command. In short, they are percentages of the time used by the processor for:
How can we know all this? Simple, man top
. The ones I look at first are id
le and wa
it.
Until now, we do not have a command that clearly indicates if there is a write / read activity on the swap. The one I use frequently is vmstat' with the arguments
-n 1', which means that it shows periodically, every 1 second, the information:
Obviously, for what we were discussing, the swap column is of interest. si means swap in, i.e. blocks transferred from memory to disk. so means swap out, i.e. blocks brought from disk (swap) into memory to be used.
If you're curious, the next two columns shows io/bi bo are disk blocks in / out, i.e write/read on disks.
On a system that is swapping, the rate of change over time in so is the important parameter to follow, with the idea that swap-in is not serious as long as there is not much swap-out.
For the other numbers and information from the screenshot above, I hope you already know, man vmstat
. Some parameters are the same as at the top (cpu / us sy id wa st).
This is a kernel parameter describing how aggressive the kernel will try to use swap. The swappiness value can range from 0 to 100, the default value is 60. Higher values will increase aggressiveness, lower values decrease the swap usage. A value of 0 instructs the kernel not to initiate swap until critical.
If you want to check/change this:
cat /proc/sys/vm/swappiness # show the value
echo 10 >/proc/sys/vm/swappiness # set the value to 10
sysctl vm.swappiness=10 # another way to set the value to 10
# edit /etc/sysctl.conf to make it persistent
As a side note, you don't need to reboot or restart nothing after a change in /proc/sys
or sysctl
, those values will be noticed by the kernel on-the-fly.
The Nagios monitoring system has a plugin check_swap
, usually at /usr/lib/nagios/plugins/check_swap
.
The default result is used on a machine without swap is CRITICAL
but the plugin offers a flag to change that:
/usr/lib/nagios/plugins/check_swap --allswaps --no-swap=ok -w 30% -c 20%
This means:
As you can see, it does monitor how much swap space is available, not, as I have said above, the "swap out" activity which is more relevant.
Viorel Anghel has 20+ years of experience as an IT Professional, taking on various roles, such as Systems Architect, Sysadmin, Network Engineer, SRE, DevOps, and Tech Lead. He has a background in Unix/Linux systems administration, high availability, scalability, change, and config management. Also, Viorel is a RedHat Certified Engineer and AWS Certified Solutions Architect, working with Docker, Kubernetes, Xen, AWS, GCP, Cassandra, Kafka, and many other technologies. He is the Head of Cloud and Infrastructure at eSolutions.