A VPS without swap has a hard edge: when memory runs out, the kernel kills the biggest process — usually the database — and the site goes down. Swap gives the server somewhere to put rarely used memory pages instead, turning a crash into a slowdown. Most VPS images ship without it, and adding it takes two minutes.
Check whether you have any
free -h
swapon --showIf Swap: shows 0B and swapon --show prints nothing, there is none.
Create a swap file
Size guidance: equal to RAM for servers up to 4 GB; half of RAM above that; 2 GB is enough on most web servers regardless. Here, 2 GB:
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfilechmod 600 matters: swap can contain fragments of anything that was in memory, including passwords. If fallocate is unavailable on your filesystem, dd does the same job more slowly:
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progressVerify:
free -hMake it permanent
The swap file is active now but forgotten at reboot. Add it to /etc/fstab:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstabTest the line without rebooting: sudo swapoff /swapfile && sudo swapon -a && free -h.
Set swappiness
Swappiness (0–100) is how eagerly the kernel moves pages to swap. The default of 60 is tuned for desktops and makes a server swap out application memory while there is still RAM free, which is slow. For a web server, 10 is right: swap is used only under real pressure.
sudo sysctl vm.swappiness=10
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.confA related knob, vm.vfs_cache_pressure=50, makes the kernel hold on to directory and file metadata longer — helpful for PHP applications that stat thousands of files per request.
What swap is not
It is not more RAM. Swap on NVMe is still a hundred times slower than memory; a server that is constantly swapping is a server that needs more RAM or a lighter workload. Check whether that is you:
vmstat 1 5Look at the si and so columns (swap in/out per second). Occasional small numbers are fine. Sustained hundreds or thousands mean memory is genuinely short. Find the hungry processes:
ps aux --sort=-%mem | head -8On a PHP server the fix is usually fewer PHP-FPM workers (tuning PHP-FPM pools) or a smaller MySQL buffer pool; on a small VPS it is the next plan up.
Removing or resizing
sudo swapoff /swapfile
sudo rm /swapfile
# then recreate at the new size, and update /etc/fstab if the path changedzram: the alternative on small VPSs
On a 1 GB server, compressing memory in RAM (zram) can be better than swapping to disk. Ubuntu: sudo apt install zram-tools, then set PERCENT=50 in /etc/default/zramswap. It gives roughly 1.5–2× effective memory for the pages that compress well, at some CPU cost.
VPSPioneer managed VPS plans are provisioned with swap and swappiness set as above, and memory is one of the metrics we alert on — a server that starts swapping heavily gets a ticket from us suggesting what to change before it becomes an outage.