Last Updated on 03/13/2014 by dboth
Swap space is a common aspect of computing today, regardless of operating system. Linux uses swap space and can use either a dedicated swap partition or a file on a regular filesystem or logical volume.
Types of memory
There are two basic types of memory in a typical computer.
Random Access Memory (RAM) is used to store data and programs while they are being actively used by the computer. Programs and data cannot be used by the computer unless they are stored in RAM. RAM is volatile memory; that is, the data stored in RAM is lost if the computer is turned off.
Hard drives are magnetic media used for long term storage of data and programs. Magnetic media is nonvolatile; the data stored on a disk remains even when power is removed from the computer. The CPU cannot directly access the programs and data on the hard drive; it must be copied into RAM first and that is where the CPU can access its programming instructions and the data to be operated on by those instructions.
During the boot process a computer copies specific operating system programs such as the kernel and init or systemd, and data from the hard drive into RAM where it is accessed directly by the computer’s processor, the CPU (Central Processing Unit).
The primary function of swap space is to substitute disk space for RAM memory when real RAM fills up and more space is needed.
For example, assume you have a computer system with 2GB of RAM. If you start up programs that don’t fill that RAM, everything is fine and no swapping is required. But say the spreadsheet you are working on grows when you add more rows to it, and it now fills all of RAM. Without swap space available, you would have to stop work on the spreadsheet until you could free up some of your limited RAM by closing down some other programs.
Swap space, however, allows the use of disk space to substitute for RAM which is not available. The kernel uses a memory management program that detects blocks, aka pages, of memory in which the contents have not been used recently. The memory management program swaps enough of these relatively infrequently used pages of memory out to a special partition on the hard drive specifically designated for “paging” or swapping. This frees up RAM and makes room for more data to be entered into your spreadsheet.
Those pages of memory swapped out to the hard drive are tracked by the kernel’s memory management code and can be paged back into RAM if they are needed.
The total amount of memory in a Linux computer is the RAM plus swap space and is referred to as virtual memory.
Types of Linux swap
Linux provides for two types of swap space. By default, most Linux installations create a swap partition, but it is also possible to use a specially configured file as a swap file.
A swap partition is just what its name implies—a standard disk partition that is designated as swap space by the mkswap command.
Refer to the page, Adding more swap space, for a complete description of how to add a new swap partition to an existing one in order to expand the amount of swap space available to a system.
A swap file can be used if there is no free disk space in which to create a new swap partition or space in a volume group in which a logical volume can be created for swap space. This is just a regular file that is created and preallocated to a specified size. Then the mkswap command is run to configure it as swap space. I don’t recommend using a file for swap space unless absolutely necessary.
Thrashing can occur when total virtual memory, both RAM and swap space, become nearly full. The system spends so much time paging blocks of memory between swap space and RAM and back, that little time is left for real work. The typical symptoms of this are fairly obvious.
- The system becomes completely unresponsive or very, very slow.
- If you can issue a command like free that shows CPU load and memory usage, you will see that the CPU load is very high, perhaps as much as 30 to 40 times the number of CPU cores in the system.
- Both RAM and swap space are almost completely allocated.
After the fact, looking at SAR data can also show these symptoms. I install SAR on every system I work on and use it for post repair forensic analysis.
What is the right amount of swap space?
Many years ago, the rule of thumb for the amount of swap space that should be allocated was 2X the amount of RAM installed in the computer. Of course that was when computers typically had RAM amounts measured in KB or MB. So if a computer had 64KB of RAM, a swap partition of 128KB would be an optimum size.
This rule of thumb took into account the fact that RAM memory sizes were typically quite small at that time and the fact that allocating more than 2X RAM for swap space did not improve performance. With more than twice RAM for swap, most systems spent more time thrashing than actually performing useful work.
RAM memory has become quite inexpensive and most computers these days have amounts of RAM that extend into tens of Gigabytes. Most of my newer computers have at least 4 or 8GB of RAM and one has 32GB. My older computers have from 1 to 4 GB of RAM.
When dealing with computers having huge amounts of RAM, the limiting performance factor for swap space is far lower than the 2X multiplier. The Fedora 19 Installation Guide, which can be found at Fedora documentation, contains the following text and a table that defines current thinking about swap space allocation.
Swap partitions support virtual memory: data is written to a swap partition when there is not enough RAM to store the data your system is processing.
In years past, the recommended amount of swap space increased linearly with the amount of RAM in the system. Modern systems often include hundreds of gigabytes of RAM, however. As a consequence, recommended swap space is considered a function of system memory workload, not system memory.
The following table provides the recommended size of a swap partition depending on the amount of RAM in your system and whether you want sufficient memory for your system to hibernate. The recommended swap partition size is established automatically during installation. To allow for hibernation, however, you will need to edit the swap space in the custom partitioning stage.
I have included below the table of recommendations from that document. Note that versions of this table from earlier Fedora distributions differ from Fedora 19 in some aspects.
|Amount of RAM installed in system
|Recommended swap space
|Recommended swap space with hibernation
|2GB – 8GB
|8GB – 64GB
|No extra required.
Of course most Linux administrators have their own ideas about the appropriate amount of swap space—as well as pretty much everything else. Table 2, below, contains my own recommendations based on my experiences in multiple environments.
|Amount of RAM installed in system
|Recommended swap space
|2GB – 8GB
The main consideration in both tables is that as the amount of RAM increases, adding more swap space simply leads to thrashing well before the swap space even comes close to being filled. If you have too little virtual memory while following these recommendations, you should add more RAM rather than more swap space.