Swap space
From Sysadmin
Swap Space (often informally known simply as swap) is an area of secondary storage that is used as virtual memory. Swap space may be stored on hard drives or even on USB keys. In modern systems a swap space may be either a swap partition or a swap file.
In general the memory available to systems has grown more quickly than application demand for memory. In the 1980s and 1990s systems could be expected to swap a little during normal utilisation as demand often exceeded available memory. Today the memory available to systems is often sufficient for their regular needs. As a result systems often swap less than they did in the past. This does not, however, abrogate the need for swap space.
Debate over Use of Swap
In recent years a debate over the use of swap on modern systems has ensued. Some argue that since the memory available to systems has grown enormously in recent years that swap space is unnecessary. Some even claim it will slow down the system (as a result of the need to swap).
In general, modern systems can and should have enough memory that they do not swap appreciably under normal conditions. It is recommended to continue using swap space on general purpose servers anyway.
Swap has three principal advantages even in modern systems:
- Swap space allows a system to exchange rarely used memory for commonly used memory. By swapping out rarely used memory more RAM is available for other tasks, such as disk caching. This is a net win for the system.
- The use of swap space also makes an out of memory (OOM) condition much less likely. An OOM condition is highly undesirable as it puts the system in an indeterminate state.
- The virtual memory subsystems on some operating systems may implicitely assume that swap space is present. Even if the OS in question works without swap space, the virtual memory management may work better if swap is available.
An alternative to not using swap space at all, is to enable it but to tune the system so that it only swaps on demand. With Linux this can be achieved by setting swappiness to 0.
Swap Allocation
Some old versions of Unix required that a system have twice as much swap as system memory. This is no longer the case with any modern version of Unix. Memory and disk access speeds have increased dramatically but they have not kept up with the growth of disk capacity or the size of applications. This means that a modern system starts to thrash when it is swapping a proportionally smaller amount of its total system memory. The upshot is that most systems need proportionally less swap than they did before.
Recommendation
The amount of swap required for a system is determined by comparing the application requirements to the amount of system memory available. Some additional swap is added to allow for unused applications to be swapped out (increasing file buffers and disk cache) and allowing for unexpected events.
On modern Unix systems (other than laptops wanting to hibernate) it is recommended that swap space not exceed 50% of system memory and not more than 4GB regardless of how much memory the system has.
Unix laptops may need 100-200% of system memory as swap to allow for hibernation.
