sections in this module City College of San Francisco - CS260A
Unix/Linux System Administration

Module: Filesystems II
module list

Swapping

Before we discuss swapping, we must review the traditional method of running multiple programs on Unix:

When a program was started, it was copied from disk to memory in its entirety. If another program needed to be run at the same time, memory was taken from program(s) currently in memory but unable to execute until an external event completed, to give to the new program being copied in. This required saving the previous programs' state temporarily to free their memory.

The term swap refers to saving an entire executing program to disk to release the memory it is using for another program. This was traditionally how systems executed multiple programs, due to severe memory limitation. Modern operating systems do not swap. Instead, they page. The paradigm for current Unix and linux systems is then

When a program is started, a sufficient part of it to begin execution is brought into memory. That portion remains until a reference is made to a non-resident piece. This reference, called a page fault, causes more of the program to be brought it. If memory is needed, a search is made of the programs in memory to find page(s) that have not been referenced. If the page has been modified (i.e., it is data), it is paged-out, saving it in the paging area on disk. When it is needed again, it is read in (or paged-in) from the paging area.

This paging area is still called the swap area. Paging's much more efficient use of memory greatly increases the number of programs that can be executed simultaneously. It also decreases the amount of data that must be transferred when programs are moved between disk and memory. However, it greatly increases the complexity of the bookkeeping and monitoring code and the number of interrupts that must be processed. Thus it took a long time to get the technique to work reliably and efficiently.

This section is about 'swapping', which we know should more correctly be called paging. It is simply an overview. If you find your system is paging much, you should consider adding more memory, as it is so cheap by historical standards. However, there are two types applications that require a lot of paging

A swap area can greatly increase system capabilities and lessen the chances that your system could run out of memory. However, memory-starved systems, even with sufficient swap space, perform poorly. If your system's performance is not what you would expect, one issue to examine is the paging performance. Memory-starved systems can degrade to the point where paging occurs constantly. This situation, called thrashing, eats up much of your I/O bandwidth and can bring the system virtually to a halt.

Note: Swapping performance is an advanced topic. This section is meant as an introduction to the issues. If your system has significant paging performance issues, you will need to do more research.

Swap areas

It is not necessary to have a swap area. Without one, your system is limited to the physical memory on hand. This means that if executing programs use much data, it is much easier to run out of memory. It is thus recommended to always have one swap area, even if it is seldom used.

Example: the system this document is being produced on has 2GB of RAM. Currently, it is running 123 processes, including Xwindows, an HTML editor, a browser, OpenOffice.org, and multiple terminals. The combined total memory size (vmsize) of those programs is 1.24GB. However, only 437MB of physical memory is currently in-use (the run-set-size). Thus, in the 4 hours the system has been up, it has never used the swap area. This means that there is sufficient room to keep in memory all of the data in use by each program, and sufficient code for each program to satisfy its immediate needs.

A swap area is usually a dedicated disk partition. The rule of thumb is 1-2 times the size of your memory. On some systems, three times the size of memory may be useful. 

Usually, the swap area is created when the system is installed, and is never altered. On linux, the swap areas are indicated as entries in the fstab. My entry looks like this:

LABEL=SWAP-hda9       swap           swap    defaults        0 0

Note the lack of a mount point. Swap areas are not mounted. The equivalent of mounting a swap area is referred to as initiating swapping.

Multiple swap areas

One swap area is sufficient for personal systems. For applications where swapping is frequent, multiple swap areas may increase paging performance, particularly if the areas are on different physical devices. 

To create a second swap area, you can

Then you can assign priorities to your swap space to have the system favor the fastest area over the slowest area.

A 'swap file', which is useful for additional, slower, swap space, must be zeroed before it is used. This can be done using a dd command whose input is /dev/zero. For example, to create a swap file whose path is /spare/swap of (about) 1GB,

# dd if=/dev/zero of=/spare/swap bs=1024 count=1048576

(There are references to the swap file produced above 'not having any holes'. As dd does not guarantee contiguous space, I'm not sure what this means, but each reference to creating swap files that I found used dd to zero it.)

Note that the swap area has not yet been formatted for swap. That is next.

Formatting for swap

Once the partition or zeroed file has been prepared, create the swap area using mkswap

mkswap [ -L label ] { device | file }

In our example of a swap file,

mkswap /spare/swap

Adding the -L option adds the label to the swap device. This is highly recommended for swap partitions or volumes and probably not applicable for swap files.

Enabling the swap space

Last, simply turn on the swap area. The area can also be assigned a priority, but this is not required. Linux seems to correctly prioritize swap partitions over swap files by itself. (Swapping from a swap partition is faster than swapping from a swap file.)

swapon { -L label | device | file }

In our example of a swap file,

swapon /spare/swap

To automate the enabling of swap devices, add the swap area to the fstab. In the case of a swap file, the path to the file is substituted for the swap device. In our swap file example

/spare/swap      swap          swap    defaults        0 0

Once in the fstab, all swap space can be enabled with swapon -a

Disabling swap space

swapoff {-a | device | -L label | filepath}

Controlling swap tendancy

As we indicated previously, most single-user modern Linux systems rarely swap. Between having sufficient memory and using dynamic paging they rarely become memory starved. There are circumstances, however, in which more (or less) swapping might be desirable. This can be tuned with the swappiness parameter.

swappiness has a value between 0 and 100. It controls the tendency of the kernel to swap pages out when they are unused. swappiness=0 says 'swap less'. swappiness=100 says 'swap more'. Of course, swapping more means you will have more free memory on average.

You can tune the swappiness parameter by echoing a new value to /proc/sys/vm/swappiness:

# echo 100 > /proc/sys/vm/swappiness
# cat /proc/sys/vm/swappiness
100

Even with a swappiness of 100 on my Redhat system with just 2GB RAM running OpenOffice, Mozilla, Kompozer, and Acrobat Reader, I couldn't get the thing to swap.

Examining swap areas

Simply cat /proc/swaps to see your active swap areas. You can check to see how much your system is paging using vmstat. In the output, si indicates swap in and so indicates swap out (actually, page-in/page-out)

vmstat 1 2

Remember, the first line output by vmstat is the totals since system boot. The second line (here taken at a one-second interval) is a snapshot. If you want to watch the progression of system activity, simply increase the second number to how many measurements you want.

You can also use the top command to view swap information.


Prev This page was made entirely with free software on linux:  
the Mozilla Project
and Openoffice.org      
Next

Copyright 2012 Greg Boyd - All Rights Reserved.