Swap (swap space) is an area allocated on the hard drive that Linux uses to supplement physical memory (RAM). When RAM becomes full, the kernel writes rarely used memory pages to the swap area, and the memory thus freed up is allocated to the currently active programs. If the data that has been written out is needed again later, the kernel reads it back into memory.
Swapping is part of virtual memory management: programs are unaware that some of their data is currently stored on the hard drive. This comes at the cost of speed, as the hard drive – even a fast NVMe SSD – is orders of magnitude slower than RAM.
What is it good for, and what isn’t it good for?
Swap is primarily a safety reserve. If a server runs out of memory for a short time (for example, during a traffic peak or a large backup), without swap the kernel’s OOM killer will immediately terminate a process, often the database itself. With swap, the system slows down but can weather the peak. The kernel may also write memory pages that have not been used for a long time to swap (for example, data from a service loaded at boot-time but which has been idle ever since), even if there is still free RAM available, thus freeing up more space for the file cache.
Swap, however, is no substitute for RAM. If the system is constantly writing to and reading from swap, response times will increase dramatically. This is a sign that the server needs more memory.
Swap partition or swap file
- Swap partition: a separate partition on the hard drive, usually created during installation.
- Swap file: an ordinary file on the file system. It can easily be created, resized or deleted at a later date, which is why this is generally the more practical option on a VPS.
In terms of performance, the two are practically equivalent on modern kernels. Our article on the OOM killer and swap files provides a step-by-step guide to creating a swap file.
Checking the swap
free -h # memória és swap: összes, használt, szabad
swapon --show # a bekapcsolt swap területek és méretük
vmstat 1 5 # az si és so oszlop a másodpercenként be- és kilapozott adat
It is not a problem in itself if the free -h output shows that some of the swap space is in use: these may be pages that have not been used for a long time. A warning sign is if the vmstat si and so columns consistently show values other than zero, meaning that the system is constantly swapping.
The swappiness setting
The vm.swappiness parameter (0–200; the default is 60 on most distributions) controls how readily the kernel will swap out data rather than flush the file cache. The current value is displayed by the command cat /proc/sys/vm/swappiness. On the test machines, Ubuntu 24.04 and Debian 12 ran with the default value of 60, whilst AlmaLinux 9 and 10 ran with a value of 30: this is set by the tuned service’s virtual-guest profile on virtual machines (tuned-adm active). On database servers, a lower value (such as 10) is often set so that the kernel only resorts to swap as a last resort.
How much swap space do I need?
There is no one-size-fits-all rule. On VPSs with limited memory (1–4 GB RAM), a swap size equal to the amount of RAM is a good starting point; on servers with more memory, a few gigabytes is usually sufficient as a reserve. If the swap space regularly fills up, do not increase the swap size; instead, increase the memory: the resources of KVM VPS plans can be upgraded at a later date. You can find further practical advice in our blog post on swap files.