Find us on social media
SWAPMemoryPerformance4 min read
Configure SWAP memory
Create and configure SWAP memory to prevent processes from being killed due to low RAM on your VPS.
SWAP provides overflow space when physical RAM is full. It prevents out-of-memory crashes on VPS instances with limited RAM.
Step 1 — Check current swap
bash
sudo swapon --show
free -hIf no output from swapon, no swap is configured.
Step 2 — Create a swap file
Recommended sizes:
- 1 GB RAM → 2 GB swap
- 2 GB RAM → 2 GB swap
- 4 GB RAM → 4 GB swap
- 8+ GB RAM → 4 GB swap
bash
sudo fallocate -l 2G /swapfileIf fallocate is not available:
bash
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048Step 3 — Set permissions and format
bash
sudo chmod 600 /swapfile
sudo mkswap /swapfileStep 4 — Enable the swap
bash
sudo swapon /swapfileVerify:
bash
free -hStep 5 — Make it permanent
Add to /etc/fstab so it persists after reboot:
bash
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstabStep 6 — Adjust swappiness
Swappiness controls how aggressively the kernel uses swap (0-100). For a VPS, 10 is recommended:
bash
sudo sysctl vm.swappiness=10Make it permanent:
bash
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.confStep 7 — Adjust cache pressure
bash
sudo sysctl vm.vfs_cache_pressure=50
echo 'vm.vfs_cache_pressure=50' | sudo tee -a /etc/sysctl.confRemove swap (if needed)
bash
sudo swapoff /swapfile
sudo rm /swapfileRemove the line from /etc/fstab.
Note
Swap on SSD/NVMe (like Baires Host VPS) is fast but still slower than RAM. Use swap as a safety net, not as a substitute for adequate RAM. If your VPS consistently uses swap, consider upgrading your plan.
Was this guide helpful?