Managing swap space on Linux systems

How to collect Linux performance statistics and use them to right-size swap files and partitions

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.

[ Prove your expertise with the free OS in InfoWorld's Linux admin IQ test round 1 and round 2. | Track the latest trends in open source with InfoWorld's Open Sources blog and Technology: Open Source newsletter. ]

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:

1 2 Page 1
From CIO: 8 Free Online Courses to Grow Your Tech Skills