Skip to main content
Swap is a file on disk that the system uses as “spare” memory when RAM runs out. Without swap, a process that runs out of memory gets killed by the OOM-killer — the service crashes. With swap, the kernel pushes inactive pages out to disk and the process keeps running.
Swap is not a replacement for RAM. Disk is tens of times slower than RAM. Swap saves you from sudden crashes and smooths out spikes, but if the server constantly “lives” in swap, that’s a sign it’s genuinely short on memory.

Check the current state

First, see whether swap already exists and how much free memory there is:
free -h
swapon --show
free -h shows the Mem (RAM) and Swap lines. If swapon --show outputs nothing, there’s no swap.

Create a swap file

Connect to the server over SSH as root (the IP and password are in the server card in the bot) and run the commands step by step.
1

Create a file of the size you need

The quick way is fallocate:
fallocate -l 2G /swapfile
If fallocate isn’t available or the file system doesn’t support it, use dd:
dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
count=2048 with bs=1M gives exactly 2 GB.
2

Lock out access for others

The swap file must be accessible to root only, otherwise the system will refuse to enable it:
chmod 600 /swapfile
3

Format it as swap

mkswap /swapfile
4

Enable it

swapon /swapfile
Check that swap has appeared:
swapon --show
free -h
5

Enable it at boot

So that swap is mounted after a reboot, add a line to /etc/fstab:
echo '/swapfile none swap sw 0 0' >> /etc/fstab
Check that the line was added only once: grep swapfile /etc/fstab — duplicates in fstab cause errors at boot.

How much to allocate

There’s no hard rule; here’s a rough guide:
Server RAMRecommended swap
1–2 GBequal to RAM (1–2 GB)
4–8 GB2–4 GB
16 GB and up2–4 GB is enough
The logic: on the smaller plans (VPS-0, VPS-1 with 1–2 GB of RAM) swap genuinely helps you out; on the larger ones it’s more of “insurance” against rare spikes, so there’s no point chasing size.

Tune swappiness

vm.swappiness (0–100) determines how eagerly the kernel reaches for swap. The default is usually 60. For a server, it makes sense to lower it to 10 — then swap is used only when memory really runs short, not “just in case”. Current value:
cat /proc/sys/vm/swappiness
Apply it right away (until reboot):
sysctl vm.swappiness=10
Make it permanent — add it to /etc/sysctl.conf:
echo 'vm.swappiness=10' >> /etc/sysctl.conf
Lumi’s disks are on NVMe, so swap here is noticeably faster than on regular HDD/SATA-SSD. But even fast NVMe is still slower than RAM. If you’re constantly hitting the memory ceiling and swap is “on fire”, that’s not a reason to inflate the file — it’s a reason to upgrade your plan through support.

Where to next

Run an LLM on the server

Ollama and local language models on a VPS.

VPS not working

Connection issues, load, and other common situations.