Skip to main content

skip to main content

developerWorks  >  AIX and UNIX  >

System Administration Toolkit: Swap space management and tricks

developerWorks
Document options
PDF format - A4

PDF - A4
145 KB

PDF format - letter

PDF - Letter
146 KB

Get Adobe® Reader®

Document options requiring JavaScript are not displayed


Rate this page

Help us improve this content


Level: Intermediate

Martin Brown (mc@mcslp.com), Freelance Writer, Author

31 Oct 2006

Configure your swap space (including adding space in an emergency) to get the most out of your system. In this article, you'll learn how to monitor your system to determine an effective swap space figure as well as examine methods for using swap space for more than just secondary random access memory (RAM).

About this series

The typical UNIX® administrator has a key range of utilities, tricks, and systems he or she uses regularly to aid in the process of administration. There are key utilities, command-line chains, and scripts that are used to simplify different processes. Some of these tools come with the operating system, but a majority of the tricks come through years of experience and a desire to ease the system administrator's life. The focus of this series is on getting the most from the available tools across a range of different UNIX environments, including methods of simplifying administration in a heterogeneous environment.



Back to top


How UNIX uses swap space

All UNIX operating systems support a system of virtual memory (VM). With a VM system, you have two primary areas that store information about the active applications and the data it uses -- the physical random access memory (RAM) and space allocated on hard drives.

The amount of applications and data that can be loaded into memory can be larger than the physical RAM in the machine with VM, because applications and information that are not actively being used are swapped to disk (hence the term swap space).

The benefit of a VM system is that physical RAM is more effectively used -- this happens because it only holds executing applications and the data being used. Because RAM is faster than hard disk space, you get the performance you need from the actively loaded applications while also benefiting from the ability to run more applications than would normally be supported.

The result is that you gain a much larger addressable memory space (4GB on 32-bit systems and significantly larger on 64-bit systems), which makes application development easier while still providing high performance.



Back to top


Setting swap space values

The amount of swap space that you configure on your machine is largely dependent on the application and operating system. You might be unable to run all of the applications that you want with too little swap space, and you might be wasting disk space that is never used with too much swap space. You might also be indirectly supporting the ability to overload your system, since too much swap space compared to too little RAM leads to bad performance, as the system spends more time swapping pages in and out than it spends actually processing.

Different UNIX variants use their swap space in different ways. Some only use swap space when the physical memory has already been used. Others use swap space for all inactive applications and unused pages. Some keep a copy of the data in all applications -- both memory and swap space. By default, some avoid using swap space and, where possible, provide a mechanism to let you tune how swap space is used. Many use a combination of these different arrangements to make the best of their swap space.

For example, within Linux®, you can adjust the swappiness of the system when using the 2.6 kernel by setting the vm.swappiness value in /etc/sysctl.conf. Higher values lead to more pages being swapped and lower values lead to more applications being kept in memory, even if they are idle.

Beyond these basics, there are a number of different points of view and rules on how you should configure VM, and how much disk space you should allocate. Some of these approaches have been summarized below:

  • Some administrators believe you should never have less swap space than physical RAM. This was a requirement with older versions of UNIX (particularly SunOS) because of the way in which VM was used. In theory, having more VM than physical RAM enables the operating system to swap out all current processes to disk when the system becomes idle, and it can speed up performance for systems because new processes can be loaded directly into RAM without having to swap applications beforehand.
  • For development systems, many administrators agree that you should have as much VM as you have RAM. The reason for this approach is that swap space is also used during a system failure as the location where the active memory is dumped; the stored memory dump is then reloaded during boot and saved as a kernel core file. If the configured swap space is less than the physical RAM, such dumps are not possible.
  • In high-performance deployment environments, you can configure as little VM as possible, on the basis that you want to keep the applications active in memory and don't need a lot of swap. Crash recovery is not required, and the ability to identify a potential lack of physical RAM (through running out of swap space) provides a warning that your application environment is not optimized, or that you need to upgrade your RAM.
  • For desktop environments, having a very large VM allocation can be to your advantage, as it enables you to run a large number of applications (many of which will probably be idle and easily swapped) making more RAM available for the active applications.

Choosing the right amount of swap space is highly dependent on the platform you are configuring -- it's intended use and how you want to cope with a lack of available VM.

For some conservative values:

  • On a desktop system, allocate at least as much swap space as physical RAM.
  • On a server, allocate at least 50 percent of your available RAM as swap space. Once you have a figure in mind, you need to configure the swap space.


Back to top


Configuring swap space

All UNIX systems have multiple types of swap space. The primary swap space is that of which is normally configured during boot time and usually set up as part of the installation of your operating system. All UNIX systems usually have one allocation of primary swap space.

How the swap space is created and configured differs from system to system. Under Linux, a disk partition used for swap is identified through the partition ID, and the partition must be initialized by the mkswap command:

$ mkswap /dev/sda2

On most traditional UNIX environments, you can use any available partition without any preparation. On HP-UX, AIX®, and Linux, you use the swapon command to add a partition to the available swap space:

$ swapon /dev/sda2

On Solaris, you use the swap command to add the swap to the system:

$ swap -a /dev/dsk/c0t0d0s2

Note that swap space added in this way will not be retained during a reboot. To add the swap space permanently to your system, you must configure one of the boot configuration files to enable the swap allocation automatically.



Back to top


Adding boot time swap space

Under most UNIX variants, swap is added automatically at boot time as part of the kernel initialization. Most use lines in the file system mount file (/etc/fstab, /etc/vfstab or /etc/filesystems) to specify the partitions to be used as swap space. You can see an example of this within the /etc/vfstab file from Solaris (see Listing 1).


Listing 1. Specifying the partitions to be used as swap space in Solaris

#device         device           mount            FS    fsck mount   mount
#to mount       to fsck          point            type  pass at boot options
#
fd              -                /dev/fd          fd    -    no      -
/proc           -                /proc            proc  -    no      -
/dev/dsk/c0d0s1 -                -                swap  -    no      -
/dev/dsk/c0d0s0 /dev/rdsk/c0d0s0 /                ufs   1    no      -
/dev/dsk/c0d0s7 /dev/rdsk/c0d0s7 /export/home     ufs   2    yes     -
/devices        -                /devices         devfs -    no      -
ctfs            -                /system/contract ctfs  -    no      -
objfs           -                /system/object   objfs -    no      -
swap            -                /tmp             tmpfs -    yes     -

Listing 2 shows an example in Linux for the /etc/fstab file.


Listing 2. Specifying the partitions to be used as swap space in Linux

# <fs>             <mountpoint>    <type>       <opts>     
<dump/pass>
/dev/sda1          /boot           ext2         noauto,noatime 1 1
/dev/sda3          /               ext3         noatime        0 0
/dev/sda2          none            swap         sw             0 0
/dev/hda1          /var/lib/mysql  ext3         noatime        1 2
/dev/hdb1          /export/data    ext3         noatime        1 2
/dev/hde1          /export/home    ext3         noatime        1 2
/dev/hde2          /var/tmp        ext3         noatime        1 2
/dev/hde3          /usr/portage/distfiles  ext3 noatime        1 2
/dev/hde4          /tmp            ext3         noatime        1 2

/dev/cdroms/cdrom0 /mnt/cdrom      iso9660      noauto,ro      0 0
none               /proc           proc         defaults       0 0

Finally, you use the /etc/swapspaces file to configure swap space partitions in AIX (see Listing 3).


Listing 3. Using the /etc/swappspaces file to configure swap space partitions

* This file is modified by the chps, mkps and rmps commands and referenced
* by the lsps and swapon commands.

hd6:
        dev = /dev/hd6

paging00:
        dev = /dev/paging00

paging01:
        dev = /dev/paging01

Rather than editing this file directly, you should use the corresponding commands or the Smitty tool to modify the file contents.

To delete additional swap, you can use the corresponding swapoff or swap -d command to disable the swap partition. However, note that you can only disable swap that is not currently in use, and it is not possible to disable the primary swap partition.



Back to top


Adding temporary swap capacity

There are times when you can run out of configured swap space partitions and, for a short period, require more VM until you can organize a more permanent solution. If you have a partition available, then you should prepare it as outlined above in the Configuring swap space section.

If you don't have a spare partition available, then you can create one or more files, which you can temporarily add into the available swap pool. Most systems even let you use swap files on NFS mounted filesystems, which can be used when you have no local disk space available.

To use a file for swap space, first create a suitable file. The mkfile> command is most appropriate here, as it provides a simple way of creating large files. You specify the filename and filesize on the command line:

$ mkfile 512m /export/data/swapfile

You can then add the file as you would a partition:

$ swap -a /export/data/swapfile
or 
$ swapon /export/data/swapfile

Swap files used in this way are never generally a long-term solution to the problem for swap space. You should configure a dedicated partition for swap space as soon as possible.



Back to top


Monitoring swap space usage

There are two aspects to monitoring swap space and how it is used: current usage and active usage. You can find an array of different tools across the UNIX variants that report available free memory and VM usage with current usage.

Under Solaris, the swap tool provides most of the swap interface functionality, including reporting usage and available devices. For example, to get a list of configured swap devices, see Listing 4.


Listing 4. Monitoring swap space usage under Solaris

$ swap -l
swapfile             dev  swaplo blocks   free
/dev/dsk/c0d0s1     102,1       8 1220928 1220928

In Listing 4, you can see that you have one swap device that is completely unused.

Under AIX, you use the lsps tool to get the same information (see Listing 5).


Listing 5. Using lsps under AIX to monitor swap space

$ lsps -a

Page Space  Physical Volume  Volume Group  Size   %Used Active Auto 
hd6         hdisk0           rootvg        2048MB     3    yes  yes

Under Linux, you use the swapon tool, as shown in Listing 6.


Listing 6. Using swapon tool under Linux

$ swapon -s
Filename                 Type            Size    Used    Priority
/dev/sda2                partition       1004052 319744  -1

With HP-UX, you use swapinfo, as shown in Listing 7.


Listing 7. Using swapinfo to monitor swap space
 
swapinfo -t 
         Kb      Kb      Kb      PCT  START/ Kb 
TYPE    AVAIL    USED    FREE    USED LIMIT RESERVE PRI NAME 
dev     2097152  0       2097152 0%     0      -      1 /dev/vg00/lvol2 
reserve         -  1468224 -1468224 
memory  3299740  1220268 2079472 37% 
total   5396892  2688492 2708400 50%    -      0      -

In each of the above examples, you can see the list of available swap space locations, their use, and get a reasonable idea of how much (or how little) the swap is being used.

These snapshot views are useful, but they only show your current usage. There is little information here on how regularly or extensively your swap space is being used. For performance reasons or when diagnosing a problem that you suspect might be swap space related, you often need a more interactive view of the memory and paging usage.



Back to top


Active VM monitoring

The vmstat command provides a long-term view of virtual usage, including providing information on the amount of memory being actively swapped between swap space and RAM. On the command line, you need to specify the interval (in seconds) to use when reporting statistics. The resulting output shows a wealth of information, and it can be used to make decisions about your VM configuration and allocation.

Listing 8 shows a sample of the output for a Solaris machine (an interval of five seconds).


Listing 8. Sample output for a Solaris machine
 
kthr      memory            page            disk          faults      cpu
 r b w   swap  free  re  mf pi po fr de sr cd cd cd cd   in   sy   cs us sy id
 0 0 0 945932 385632  9  36 13  0  1  0 100 3  0  0  0  413  172   87  0 36 63
 0 0 0 944616 384068  0   8  3  0  0  0  0  0  0  0  0  654   21   51  0  1 99
 0 0 0 944612 384064  0   0  0  0  0  0  0  0  0  0  0  596   21   52  0  1 99
 0 0 0 944612 384064  0   0  0  0  0  0  0  0  0  0  0  694   12   48  0  1 99
 0 0 0 944612 384064  0   0  0  0  0  0  0  5  0  0  0  658   11   50  0  1 99
 0 0 0 944612 384064  0   0  0  0  0  0  0  0  0  0  0  576   18   52  0  1 99
 0 0 0 944500 383988 115 850 1  0  0  0  0  0  0  0  0  612 1656  120  1  5 94
 0 0 0 942060 382444  6  62  0  0  0  0  0  0  0  0  0  669  256  146  0  2 98
 0 0 0 942048 382432  9 105  0  0  0  0  0  0  0  0  0  699  414  188  0  2 98
 0 0 0 946188 388188 481 2400 10 10 10 0 0  5  0  0  0  595 12613 6082 6 18 76
 0 0 0 944488 386308 769 4753 1 8  8  0  0  7  0  0  0  625 15705 6285 8 29 63
 0 0 0 943128 385104 839 5194 176 9 9 0  0 14  0  0  0  579 16077 6304 8 32 60
 0 0 0 937692 377628 32 209  2  0  0  0  0  2  0  0  0  654  403  131  0  2 97
 0 0 0 937228 377208  0   0  0  0  0  0  0  0  0  0  0  647   38   63  0  1 99
 0 0 0 937220 377200 121 980 0  0  0  0  0  0  0  0  0  623 1246  218  1  6 93
 0 0 0 937348 377320 126 1044 2 1  1  0  0  3  0  0  0  667 1266  228  1  7 93
 0 0 0 937428 377408 57 686  0  0  0  0  0  0  0  0  0  702  890  180  1  5 94
 0 0 0 937436 377404 72 861  0  0  0  0  0 54  0  0  0  689 1113  295  1  6 93

There are a number of columns here -- some more important than others. The key ones are:

  • swap -- shows the total of swap available
  • free -- shows the amount of free swap space
  • pi -- kilobytes paged in
  • po -- kilobytes paged out
  • fr -- kilobytes freed

A very low amount of free space and a high number for pages in and out probably indicates a lack of VM and, with very high pages in and out, probably a lack of physical RAM.



Back to top


Using swap space for temporary storage

Under some UNIX variants, notably Solaris and Linux, you can use the tmpfs filesystem; this uses VM as storage space. When you have ample RAM, this provides an excellent way of supporting very fast disk access; unused files will be swapped on to disk. The result is a highly efficient solution for fast access to the files you use most often, and it can be a significant speed boost for applications, such as software development, where a large number of temporary files are created during the compilation process.



Back to top


Summary

Getting your swap space configuration right can make a big difference to the efficiency of your machine. Most people set and forget their swap space settings but, in this article, you've examined several ways to add and adjust your swap space. You've also seen how you can permanently add swap to your configuration and how to add swap in an emergency when you run out of memory.



Resources

Learn

Get products and technologies
  • IBM trial software: Build your next development project with software for download directly from developerWorks.


Discuss


About the author

Martin Brown has been a professional writer for more than seven years. He is the author of numerous books and articles across a range of topics. His expertise spans myriad development languages and platforms -- Perl, Python, Java™, JavaScript, Basic, Pascal, Modula-2, C, C++, Rebol, Gawk, Shellscript, Windows®, Solaris, Linux, BeOS, Mac OS X and more -- as well as Web programming, systems management, and integration. He is a Subject Matter Expert (SME) for Microsoft® and regular contributor to ServerWatch.com, LinuxToday.com, and IBM developerWorks. He is also a regular blogger at Computerworld, The Apple Blog, and other sites. You can contact him through his Web site.




Rate this page


Please take a moment to complete this form to help us better serve you.



 


 


Not
useful
Extremely
useful
 


Share this....

digg Digg this story del.icio.us del.icio.us Slashdot Slashdot it!



Back to top


IBM, AIX, and AIX 5L are registered trademarks of International Business Machines Corporation in the United States, other countries, or both. Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. in the United States, other countries, or both. Linux is a trademark of Linus Torvalds in the United States, other countries, or both. Microsoft, Windows, Windows NT, and the Windows logo are trademarks of Microsoft Corporation in the United States, other countries, or both. UNIX is a registered trademark of The Open Group in the United States and other countries. Other company, product, or service names may be trademarks or service marks of others.