Managing swap space on Linux systems

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

RELATED TOPICS
Page 2 of 2
# sar -r | head -6
Linux 2.6.18-128.el5 (boson)     06/17/2012

12:00:01 AM  ...  %memused  ...   kbswpfree   kbswpused   %swpused 
12:10:01 AM  ...    35.01   ...   16777208        0          0.00      
12:20:01 AM  ...    34.97   ...   16777208        0          0.00      
12:30:01 AM  ...    34.91   ...   16777208        0          0.00      

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  ...  %memused  ...   kbswpfree   kbswpused   %swpused 
12:10:01 AM  ...    99.74   ...      0         8032460     100.00   
12:20:01 AM  ...    99.73   ...      0         8032460     100.00   
12:30:01 AM  ...    99.74   ...      0         8032460     100.00 
12:40:01 AM  ...    99.74   ...      0         8032460     100.00  
12:50:01 AM  ...    99.74   ...      0         8032460     100.00 
01:00:01 AM  ...    99.74   ...      0         8032460     100.00 

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.

RELATED TOPICS
| 1 2 Page 2
From CIO: 8 Free Online Courses to Grow Your Tech Skills
View Comments
Join the discussion
Be the first to comment on this article. Our Commenting Policies