VPS Swap Space: How to Configure and Optimize Swap on Linux VPS

Swap space is a critical but often overlooked part of VPS performance. When your VPS runs out of physical RAM, the kernel moves idle memory pages to swap preventing out-of-memory crashes. Proper swap configuration keeps your server stable without sacrificing performance.

You can compare VPS plans with enough RAM for your workload on our VPS comparison page.

How Much Swap Do You Need?

RAM SizeRecommended SwapWith Hibernation
512 MB1 GB1 GB
1 GB1 GB2 GB
2 GB1 GB3 GB
4 GB2 GB5 GB
8 GB+2 GBRAM x 1.5

Creating Swap on a Linux VPS

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Optimization

Set swappiness to 10 so the kernel only swaps when RAM is over 90% full, ideal for VPS environments: sudo sysctl vm.swappiness=10. Check our VPS hosting comparison for plans with adequate RAM.

Leave a Reply