Linux Swap Space
You should almost always add swap space to Linux servers
Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space. While swap space can help machines with a small amount of RAM, it should not be considered a replacement for more RAM. Swap space is located on physical drives, which have a slower access time than physical memory.
There are performance benefits when swap space is enabled, even when you have more than enough ram
Even if there is still available RAM, the Linux Kernel will move memory pages that are hardly ever used into swap space.
It’s better to swap out memory pages that have been inactive for a while, keeping often-used data in cache, and this should happen when the server is most idle, which is the aim of the Kernel.
Avoid setting your swap space too large if it will result in prolonging performance issues, outages, or your response time (without proper monitoring/alerts).
If you don’t have enough memory, swap will be used quite often and noticeably more during any memory requirement spikes. If you don’t have enough memory and no swap space, this will often cause failure to allocate memory for requests needing more memory pages. As a last resort, the Kernel will deploy out-of-memory OOM killer to nuke high-memory processes (usually MySQL, java, etc.).
Swap I/O scales very poorly. If memory pages cannot be swapped only when the server is idle, you should tune or disable swap.
With swap disabled, performance issues become noticeable very fast, and the OOM killer may get you.
This article has a reference from Hayden James's Linux blog Why You Should Almost Always Add Swap Space.
For a more detailed look at Linux swap space, read the Swap Management and Page Frame Reclamation chapters from Kernel.org docs.
Last updated