Managing Swap Space In Linux

Swap space is an area of disk that is used to store memory to. Here we’ll take a look at managing swap space in Linux by determining what is actually using the swap space, controlling how frequently swap space is written to, and finally adding additional swap space if required.

What Is Using Swap Space?

There are a few things we can do to check our memory and swap usage, however seeing what is actually using the swap space is another question.

My favourite tool for getting this sort of information is ‘smem’.

First we’ll install smem in our CentOS 7 server, which comes from the EPEL repository.

[root@centos7 ~]# yum install epel-release -y

[root@centos7 ~]# yum install smem -y

The smem package is also available for installation in Debian based Linux distributions as well.

root@debian8:~# apt-get install smem -y

Now we can make use of smem, which will show us how much swap space our processes are using.

[root@centos7 ~]# smem -p -t
  PID User     Command                         Swap      USS      PSS      RSS
  896 root     /sbin/agetty --noclear tty1    0.00%    0.00%    0.00%    0.02%
  889 root     /usr/sbin/irqbalance --fore    0.00%    0.01%    0.01%    0.03%
 2699 apache   /usr/sbin/httpd -DFOREGROUN    0.00%    0.00%    0.01%    0.09%
 2703 apache   /usr/sbin/httpd -DFOREGROUN    0.00%    0.00%    0.01%    0.10%
 2704 apache   /usr/sbin/httpd -DFOREGROUN    0.00%    0.00%    0.01%    0.10%
 2702 apache   /usr/sbin/httpd -DFOREGROUN    0.00%    0.01%    0.01%    0.10%
  855 root     /sbin/auditd -n                0.00%    0.01%    0.02%    0.05%
  892 root     /usr/sbin/crond -n             0.00%    0.02%    0.02%    0.04%
  890 root     /usr/lib/systemd/systemd-lo    0.00%    0.02%    0.02%    0.04%
 2700 apache   /usr/sbin/httpd -DFOREGROUN    0.00%    0.01%    0.02%    0.13%
  881 dbus     /bin/dbus-daemon --system -    0.00%    0.02%    0.02%    0.05%
 1153 root     /usr/sbin/wpa_supplicant -u    0.00%    0.02%    0.03%    0.07%
 2701 apache   /usr/sbin/httpd -DFOREGROUN    0.00%    0.01%    0.03%    0.14%
 2608 root     /usr/libexec/postfix/master    0.00%    0.03%    0.03%    0.06%
 1461 root     /usr/sbin/sshd -D              0.00%    0.02%    0.03%    0.09%
 2636 root     -bash                          0.00%    0.03%    0.04%    0.06%
 2898 postfix  pickup -l -t unix -u           0.00%    0.03%    0.04%    0.10%
 2611 postfix  qmgr -l -t unix -u             0.00%    0.03%    0.04%    0.10%
  725 root     /usr/lib/systemd/systemd-jo    0.00%    0.03%    0.05%    0.09%
 2632 root     sshd: root@pts/0               0.00%    0.06%    0.07%    0.15%
 2698 root     /usr/sbin/httpd -DFOREGROUN    0.00%    0.06%    0.08%    0.22%
  753 root     /usr/lib/systemd/systemd-ud    0.00%    0.08%    0.09%    0.11%
  742 root     /usr/sbin/lvmetad -f           0.00%    0.09%    0.09%    0.12%
  880 root     /usr/sbin/rsyslogd -n          0.00%    0.10%    0.11%    0.15%
  950 root     /usr/sbin/NetworkManager --    0.00%    0.11%    0.14%    0.21%
    1 root     /usr/lib/systemd/systemd --    0.00%    0.15%    0.15%    0.18%
 3126 root     python /usr/bin/smem -p -t     0.00%    0.17%    0.18%    0.23%
 1154 polkitd  /usr/lib/polkit-1/polkitd -    0.00%    0.25%    0.27%    0.34%
 1462 root     /usr/bin/python -Es /usr/sb    0.00%    0.28%    0.32%    0.42%
 1273 root     /sbin/dhclient -d -q -sf /u    0.00%    0.35%    0.35%    0.41%
  891 root     /usr/bin/python -Es /usr/sb    0.00%    0.45%    0.49%    0.60%
-------------------------------------------------------------------------------
   31 5                                       0.00%    2.48%    2.81%    4.61%

In this example the whole swap column is 0 as we’re not using any at the moment. Here we use the -t option to display the totals at the bottom, and the -p option to display the swap space usage as a percentage.

We can also use the -u flag to see which users are using memory and swap space on the Linux system.

[root@centos7 ~]# smem -p -t -u
User     Count     Swap      USS      PSS      RSS
dbus         1    0.00%    0.02%    0.02%    0.05%
postfix      2    0.00%    0.06%    0.08%    0.20%
apache       6    0.00%    0.04%    0.10%    0.65%
polkitd      1    0.00%    0.25%    0.27%    0.34%
root        21    0.00%    2.15%    2.37%    3.41%
---------------------------------------------------
            31    0.00%    2.52%    2.85%    4.65%

If we install the python-matplotlib package we can then make pie and graph bars of swap usage for reporting purposes, checkout the --pie and --bar options in the manual page for further information on this.

Now that we have an idea of what is actually using the swap space, there are a few things we can do. We can investigate the specific processes and confirm if they need to be running, and also investigate any particular applications specific configuration settings further as we may be able to modify the memory allocation for example. We could also upgrade the server by adding more memory if required so that swap space does not need to be used.

If these options are not possible, we can also reduce how frequently swap space is used.

Modify Swapping Likelihood

We can set the swappiness, which is a kernel run time parameter that defines how frequently swap will be used.

The swappiness value is a kernel parameter that can be set between 0 and 100. The lower the value, the less likely the kernel is to make use of swap. The higher the value, the more likely the kernel will try to use swap. For example, setting swappiness to 100 will result in the kernel performing aggressive levels of swapping that may cause performance degradation, while setting it to 0 will only swap to avoid running out of memory.

Generally in Linux the default swappiness is set to 60, in my CentOS 7 virtual machine it appears to be 30.

[root@centos7 ~]# sysctl vm.swappiness
vm.swappiness = 30

Over on my Debian 8 virtual machine it has the usual 60.

root@debian7:~# sysctl vm.swappiness
vm.swappiness = 60

We can set the value of vm.swappiness both for the current runtime or to last persistently over reboot.

To change the current value we can simply echo a new value to /proc/sys/vm/swappiness, as shown below.

[root@centos7 ~]# echo 50 > /proc/sys/vm/swappiness
[root@centos7 ~]# sysctl vm.swappiness
vm.swappiness = 50

This change will not last a reboot, to do that we need to create a file in the /etc/sysctl.d directory which ends in .conf.

[root@centos7 ~]# echo "vm.swappiness = 1" > /etc/sysctl.d/swappiness.conf
[root@centos7 ~]# cat /etc/sysctl.d/swappiness.conf
vm.swappiness = 1

At this point we can either reboot to apply the changes, confirming that this will set vm.swappiness permanently, or we can run sysctl with the -p flag to specify the configuration file to load as shown below which will avoid the reboot.

[root@centos7 ~]# sysctl -p /etc/sysctl.d/swappiness.conf
vm.swappiness = 1
[root@centos7 ~]# sysctl vm.swappiness
vm.swappiness = 1

Now our Linux server will only use the minimum amount of swap space possible.

Add More Swap Space

If we are not able to increase memory, control the amount of memory the process may use, or change the level of vm.swappiness then you can next look at increasing the available swap space.

By default in CentOS 7 and Debian 8 the swap space comes from a logical volume, we can add more space in the form of a disk partition, LVM, or even a file. If you have the option to add a new disk partition or logical volume this is the recommended method of adding new swap space.

Here’s an example of how we can create an additional swap partition and mount it automatically at boot. I have added a new 1GB disk to the virtual machine which will be dedicated to swap space, /dev/sdb.

[root@centos7 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x48034745.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-2097151, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151):
Using default value 2097151
Partition 1 of type Linux and of size 1023 MiB is set

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

[root@centos7 ~]# mkswap /dev/sdb1
Setting up swapspace version 1, size = 1047548 KiB
no label, UUID=7ef9388f-f4b6-4732-aee0-c834297b6e06
[root@centos7 ~]# echo "UUID=7ef9388f-f4b6-4732-aee0-c834297b6e06 swap swap defaults 0 0" >> /etc/fstab
[root@centos7 ~]# swapon -a
[root@centos7 ~]# swapon -s
Filename                                Type            Size    Used    Priority
/dev/dm-1                               partition       2097148 0       -1
/dev/sdb1                               partition       1047548 0       -2

We can also make a file on the file system that acts as swap space, in this example we first create a 1GB file at /root/swapfile, format it as swap space, and then mount it.

[root@centos7 ~]# dd if=/dev/zero of=/root/swapfile bs=1024M count=1
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB) copied, 4.49015 s, 239 MB/s
[root@centos7 ~]# ls -lah /root/swapfile
-rw-r--r--. 1 root root 1.0G Aug 31 15:35 /root/swapfile
[root@centos7 ~]# mkswap /root/swapfile
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=0842421c-be65-4f7e-80c1-d2b89d880c0b

[root@centos7 ~]# echo "/root/swapfile swap swap defaults 0 0" >> /etc/fstab
[root@centos7 ~]# swapon -a
swapon: /root/swapfile: insecure permissions 0644, 0600 suggested.
[root@centos7 ~]# chmod 600 /root/swapfile
[root@centos7 ~]# swapon -s
Filename                                Type            Size    Used    Priority
/dev/dm-1                               partition       2097148 0       -1
/root/swapfile                          file    		1048572 0       -2

The priorities of our newly added swap spaces are both -2, meaning that they should only be used after the swap space at priority -1 has been exhausted.

The ‘swapon -a’ command will attempt to mount all swap space defined in the /etc/fstab file. The contents of this file are also automatically mounted at boot, so the new swap space will be there ready to use even after reboot. The ‘swapon -s’ command will list the summary of available swap space, above we can see the swap file that we have added is now in use.

We can also see a warning that recommends our swap file be set to permissions 600 rather than the default 644 file permissions, which makes sense as we likely only want the root user to have access to a file that will potentially contain sensitive parts of memory.

Summary

Ideally we would want to troubleshoot the root cause of the swap usage, or even just add more memory to a Linux system, or look at putting some sort of limitation on the processes responsible for using large amounts of RAM, however these options are not always possible.

Here we have covered how you can track down the processes and users that are actually using memory and swap space with smem, shown how you can adjust the swappiness levels to control how often the system will use swap space, and finally how we guided you through adding more swap space if required.

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>