💾
Hosting

Swap on a VPS: do you need it, how much, and how to set it up

17.11.2026
← All articles

Running out of memory is one of the most common problems on small VPS plans. The site is fine all day, but during a traffic spike or a heavy backup, RAM runs out and the Linux kernel kills the process using the most memory to save the system. Very often that is MySQL, and the site starts showing a database connection error. Swap acts as reserve memory for exactly these moments.

What swap is

Swap is disk space where the kernel temporarily moves memory pages that have not been used for a while, freeing RAM for active processes. Disk is far slower than RAM, so swap cannot replace memory, but it keeps the system standing during sudden load. On a VPS you almost always use a swap file rather than a partition, since it is easy to create, resize or remove. Many VPS images ship without any swap at all.

Checking the current state

sudo swapon --show
free -h
sudo journalctl -k | grep -iE "out of memory|oom-kill"

Empty output from swapon and zeros in the Swap row mean there is no swap. Lines like Out of memory: Killed process in the kernel log show the server has already run short and which process was killed.

When swap helps and when it does not

For VPS plans with 1 to 4 GB of RAM, swap is almost always worthwhile: it absorbs short spikes and prevents the database from dying suddenly. It should not hide a real shortage, though. Constant swapping makes everything slow and means you need more RAM or leaner settings, such as fewer PHP-FPM workers.

A learning centre in Namangan runs Nginx, PHP-FPM and MySQL on a 2 GB VPS. Each quarter, when exam results go live, hundreds of students log in at once and MySQL used to be killed. The admin added a 2 GB swap file and lowered the PHP-FPM worker limit. During the next release the site was a bit slower, but the database stayed up, and a RAM upgrade went on the roadmap.

Choosing a size

RAMSuggested swap
1 GB1-2 GB
2 GB2 GB
4 GB2-4 GB
8 GB or more2-4 GB as needed

These are rough guidelines in line with Ubuntu's documentation. Mind your free disk space: on a VPS with a small disk, a 4 GB swap file can crowd out site files and backups. Swap is easy to resize later, so start with a moderate value and adjust it after watching real usage for a while.

Creating the swap file

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

If swapon rejects a file created with fallocate on your filesystem, create it with sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 instead. To keep it after reboot, back up and append to fstab:

sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Tuning swappiness

vm.swappiness controls how eagerly the kernel swaps. It accepts values from 0 to 100 (up to 200 on newer kernels), and many distributions default to 60; for web servers a lower value such as 10 keeps active data in RAM longer:

sudo sysctl vm.swappiness=10
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf

The first command applies the value immediately; the second file makes it persist across reboots.

Monitoring

Watch swap for a few days after enabling it. Some usage is normal, because the kernel has simply moved rarely used pages out of RAM. What matters is constant traffic in and out, visible in the si and so columns of vmstat 5 5. Persistent non-zero values mean the server is short on memory: optimise your apps or move to a larger VPS plan. Questions about VPS on sayt.uz can be raised from the cabinet or via a support ticket.

Related articles

🗂️ Working with the ISPmanager File Manager: Uploads, Archives, Editing and Permissions ⏱️ 504 Gateway Timeout: what causes it and how to fix it step by step 📜 Hosting SLA Explained: Uptime Guarantees and Compensation 📚 Hosting for an OJS academic journal: requirements and setup
🌐 Language
🇺🇿 O'zbek 🇺🇿 Ўзбек 🇷🇺 Русский 🇬🇧 English ✓