Nuts and Bolts Performance Tuning Dojo 

A swap space primer

Swap Tricks

What happens when RAM runs out? By Federico Lucifredi

In Issue 7, you learned that free memory in Linux does not really exist [1]. The kernel employs all memory not in use by an application in a variety of useful activities, among which is caching the reads and buffering the writes of the storage subsystem. When an application is launched, memory pages are reclaimed from the caches for the application's own use. In the last issue, I also examined how to use the stress [2] tool to put arbitrary memory pressure on your test system. The next natural question is: What happens when memory runs out? That is, running applications are attempting to use more memory than the system possesses. An important distinction is that allocating pages alone is not sufficient; the Linux kernel is efficient, and memory pages have to be dirty with data to take up physical RAM.

When running processes request more than the system physically has, the swap space facility offers the kernel a solution for otherwise unmanageable demands. In Linux, swapping is completely transparent to the processes themselves, which typically remain unaware of where their memory pages reside. Because swapping memory pages to disk is conceptually the inverse of disk caching, then just as disk caching enhances system performance, swapping is detrimental to it, the disk being several orders of magnitude slower than RAM.

Unless the swapped pages belong to a steadily idling process or are not being used consistently, disk thrashing results as the system attempts to keep things running by continuously swapping pages in and out of RAM, bringing performance to a grinding halt.

The simplest way to investigate the configuration of swap space is to check /proc/swaps directly (Listing 1) or with the equivalent swapon -s.

Listing 1: /proc/swaps

01 $ more /proc/swaps
02 Filename   Type       Size     Used   Priority
03 /dev/sda5  partition  4085756  52864  -1

The most common configuration is a dedicated swap partition (type 0x82), but you can use swap files hosted in a general-purpose partition, up to the compile-time limit MAX_SWAPFILES predefines in swap.h. With multiple swap areas, the Priority field determines usage order. Multiple swap areas at the same priority prompt the kernel to interleave access, which may improve swap space performance if leveraged correctly.

The mkswap command can initialize either a dedicated partition or a zero-filled file as swap space then put it in active operation by swapon. Disabling swap is often required when analyzing a system's memory performance, and this can be accomplished easily with /sbin/swapoff -a (and restored with /sbin/swapon -a). Current use of the swap file is accessed with free (Listing 2).

Listing 2: Visualizing Swap Space Usage

01 $ free
02              total       used       free     shared    buffers    cached
03 Mem:       4012008    3858124     153884          0      11644    440852
04 -/+ buffers/cache:    3405628     606380
05 Swap:      4085756     353784    3731972
06 $

Linux 2.6 introduced a new swappiness parameter, ranging from 0 to 100, that influences the way the kernel makes swapping decisions. Higher values result in an increased tendency to swap to disk; lower values keep more applications in memory at the expense of other kernel caching activities. Tuning this parameter is truly a black art, but it is a trade-off between interactive response times relevant to desktop users and caching behavior more significant in server operations. The value can be configured permanently as vm.swappiness in /etc/sysctl.conf, but it can also be altered directly:

echo 60 > /proc/sys/vm/swappiness

To round up the review, I'll provide some sizing guidance. A larger swap area is generally recommended for desktops than servers, sized at up to twice the available RAM. Desktops are likely to have idling applications that can leverage swapping well, and a laptop might hibernate (suspending to disk leverages swap area storage). On servers, swap mostly ensures that intermittent conditions will not cause a crash, as performance constraints usually require swap to be an exceptional circumstance. Table 1 lists swap recommendations for Red Hat Enterprise Linux Server 6; Ubuntu server's own guidelines [3] are more conservative, illustrating how much variation is accepted across data center and cloud server platforms.

Tabelle 1: RHEL 6 Recommendations


Swap Space

Up to 4GB


4GB to 16GB


16GB to 64GB


64GB to 256GB