Managing swap space on Linux systems

When your Linux server starts feeling sluggish, you might benefit from looking at your swap space and gauging whether it's adequately sized for your system's workload. Fortunately, Linux has some handy commands for helping you get a good picture of your system's behavior.

Setting your swap space to be twice the size of your RAM is a good rule of thumb, but systems with very large amounts of memory likely won't likely need anywhere near that amount of swap. Besides, adding it later is easy if you have adequate disk space.

Before we get started with the commands, let's review how swap space is used on Linux systems. Linux uses what is called a "demand-paged virtual memory system". This means that it breaks memory into chunks which are called "pages". Imagine the squares on a chess board, each square being a page and the entire board being your memory. To see how big each of these pages is, use the getconf PAGESIZE command. You will see something like this:

$ getconf PAGESIZE

The pages on this particular Linux system are 4 KB in size. That's fairly typical.

When some process on a Linux system needs more memory, the pager will swap out (i.e., move from memory to swap space) some pages that haven't been used in a while. These pages can be moved back into memory when they're needed.

Demand paging can be contrasted with anonymous paging in which, once modified, pages must remain in RAM until the associated processes have run to completion and anticipatory paging in which a process's non-resident pages are preloaded into memory if they are likely to be referenced soon. No, it's not psychic, it's just a good algorithm for predicting the behavior of your executables.

If you suspect that your system may be having problems because of inadequate swap space, it's a good idea to track how swap space is being used. One way to do this is by using the sar command (part of the sysstat package) to collect performance data and then to remember to examine it from time to time. A cron job like this will collect performance statistics every 15 minutes between 8 AM and 6 PM Monday through Friday:

*/4 8-18 * * 1-5 /usr/lib/sysstat/sa1 -d 240 1

Some systems are preconfigured to collect performance information with sar. Check the contents of your /var/log/sa directory for files like these. If you see them, you're already collecting performance data and can examine the last week's worth of performance data with sar.

# ls -l /var/log/sa
total 24880
-rw-r--r-- 1 root root 1530096 Jun  9 23:50 sa09
-rw-r--r-- 1 root root 1530096 Jun 10 23:50 sa10
-rw-r--r-- 1 root root 1530096 Jun 11 23:50 sa11
-rw-r--r-- 1 root root 1530096 Jun 12 23:50 sa12
-rw-r--r-- 1 root root 1530096 Jun 13 23:50 sa13
-rw-r--r-- 1 root root 1519936 Jun 14 23:50 sa14
-rw-r--r-- 1 root root 1530096 Jun 15 23:50 sa15
-rw-r--r-- 1 root root 1530096 Jun 16 23:50 sa16
-rw-r--r-- 1 root root 1338864 Jun 17 20:50 sa17
-rw-r--r-- 1 root root 1475429 Jun  9 23:53 sar09
-rw-r--r-- 1 root root 1475429 Jun 10 23:53 sar10
-rw-r--r-- 1 root root 1475429 Jun 11 23:53 sar11
-rw-r--r-- 1 root root 1475429 Jun 12 23:53 sar12
-rw-r--r-- 1 root root 1475429 Jun 13 23:53 sar13
-rw-r--r-- 1 root root 1466623 Jun 14 23:53 sar14
-rw-r--r-- 1 root root 1475429 Jun 15 23:53 sar15
-rw-r--r-- 1 root root 1475429 Jun 16 23:53 sar16

You can view some of the stats collected since the beginning of the day just by typing sar. The default view will show you information on CPU stats. The lines below show this system is idle nearly all the time.

# sar
Linux 2.6.18-128.el5 (boson)     06/17/2012

12:00:01 AM       CPU     %user     %nice   %system   %iowait    %steal     %idle
12:10:01 AM       all      0.57      0.00      0.09      0.21      0.00     99.13
12:20:01 AM       all      0.08      0.00      0.04      0.01      0.00     99.87

Memory and swap space utilization measurements are available with sar's -r option. If the numbers look like this, you're in great shape. This system isn't using its swap space at all.

# sar -r | head -6
Linux 2.6.18-128.el5 (boson)     06/17/2012

12:00:01 AM kbmemfree kbmemused  %memused kbbuffers  kbcached kbswpfree kbswpused  %swpused  kbswpcad
12:10:01 AM  24071880  12965924     35.01    272816  10711820  16777208         0     0.00         0
12:20:01 AM  24086544  12951260     34.97    272912  10712384  16777208         0     0.00         0
12:30:01 AM  24106628  12931176     34.91    272980  10714716  16777208         0     0.00         0

If, on the other hand, the swap measurements look like this, your system is hurting. Here we see that swap space is 100% used.

# sar -r
Linux 2.6.18-128.el5 (fermion)     06/17/2012

12:00:01 AM kbmemfree kbmemused  %memused kbbuffers  kbcached kbswpfree kbswpused  %swpused  kbswpcad
12:10:01 AM  174864    65902776     99.74    81484   61464412  0        8032460    100.00      1668
12:20:01 AM  175460    65902180     99.73    82384   61490980  0        8032460    100.00      1052
12:30:01 AM  173696    65903944     99.74    74516   61536168  0        8032460    100.00       928
12:40:01 AM  173252    65904388     99.74    74160   61543720  0        8032460    100.00       792
12:50:01 AM  171900    65905740     99.74    74964   61540960  0        8032460    100.00       780
01:00:01 AM  172456    65905184     99.74    75056   61538788  0        8032460    100.00       780

If you need to add swap, you can choose to dedicate an unused partition to the swap space by using the mkswap command. For example:

mkswap -c /dev/sda4

Alternately, you can create a swap file and use the mkswap command to add the new swapfile to the available swap space. On newer Linux systems (Linux 2.6 systems and later), swap files perform as well as swap partitions.

dd if=/dev/zero of=/swapfile bs=1024 count=524288
swapon /swapfile

This dd command will add a .5 GB swap file (524288 is 1024 times 512) to your system. The swapon command will then put it into use. /dev/zero is a special file that provides however many null characters are needed to build the requested file. /swapfile is the file being built. The block size (bs) defaults to 512 and determines how many bytes are written at a time. Finally, count determines how many blocks will be created.

There's a similar command available if you decide that you want to stop swapping on a particular file or partition:

swapoff /swapfile

Of course, you have to add your swap file to /etc/fstab to ensure that it will continue to be used following reboots. The line should look like this:

/swapfile               swap                    swap    defaults        0 0

You can examine you current swap space with the swapon command:

# swapon -s
Filename                                Type            Size         Used    Priority
/dev/mapper/VolGroup00-LogVol01         partition       16777208        0       -1
/swapfile                               file            268435456    	0       -3

As you can see, the swap file has been added to the swap space. Use the swapon -s command at any time to see what swap partitions and swap files are in use.

This article is published as part of the IDG Contributor Network. Want to Join?

Computerworld's IT Salary Survey 2017 results
Shop Tech Products at Amazon