Swap space acts as overflow memory when your server's RAM is fully utilised. While not a substitute for adequate RAM, a swap file prevents out-of-memory (OOM) crashes on servers with limited memory.
Check Current Swap
$swapon --show$free -hIf no swap is configured, swapon --show returns no output.
Create a Swap File
Create a 2 GB swap file (adjust size as needed):
$fallocate -l 2G /swapfile$chmod 600 /swapfile$mkswap /swapfile$swapon /swapfileVerify swap is active:
$swapon --showNAME TYPE SIZE USED PRIO
/swapfile file 2G 0B -2Make Swap Permanent
Add the swap file to /etc/fstab so it persists across reboots:
$echo '/swapfile none swap sw 0 0' | tee -a /etc/fstabTune Swappiness
The vm.swappiness kernel parameter controls how aggressively the kernel uses swap (0–100). A value of 10 is recommended for servers:
$sysctl vm.swappiness=10$echo 'vm.swappiness=10' | tee -a /etc/sysctl.confRecommended Swap Sizes
| RAM | Recommended Swap |
|---|---|
| 1 GB | 2 GB |
| 2 GB | 2 GB |
| 4 GB | 2 GB |
| 8 GB | 4 GB |
| 16 GB+ | 4–8 GB |
[!NOTE] NVMe SSD-backed servers have fast swap performance. However, excessive swap usage indicates your server needs more RAM — consider upgrading your plan if swap is consistently in use.
