Knowledge Base:Is swap space really necessary

Synopsis
During a Linux installation, the Gentoo Handbook recommends to setup a swap space (preferably a partition) for the system.

We do get the question often if swap space is needed?

Environment
This article applies to all Gentoo Linux systems

Analysis
Linux uses a concept of virtual memory, causing applications to think they have access to all possible memory. Even more, they can see an entire address space at their disposal, regardless how much memory is really available in the system. This concept allows for simplified application development (as developers do not need to care that their programs will actually run simultaneously with others), isolation, etc. This also means that an applications' virtual memory needs to be mapped somehow to real, available memory. This is done by the Linux kernel virtual memory management subsystem.

When the sum of all running applications (and their memory requirements) and the Linux kernel itself (as it also has need for memory) exceed the available physical memory in the system, then the system is in trouble: applications still see lots of free (virtual) memory, but there is shortage on real memory. When that happens, the Linux kernel has two choices
 * 1) if swap space is available, it will move some data from RAM to the swap space
 * 2) if swap space is not available, it will start killing applications to free memory

Resolution
It is not possible to give a clear cut answer to the question "Is swap space really needed". Of course, if you have very few memory available, then you'll probably need swap space to have some comfort when working on your system. On the other side of the spectrum, if you have lots of memory available and are not running many memory-intensive applications, then you might never hit the need to move information to the swap space.

External resources

 * All available memory is being used entry on the Gentoo Wiki Knowledge Base