Start typing to search across invoices, services, domains, tickets, and more...
On a VPS with little RAM, compiling software, running a database or a sudden traffic spike can exhaust memory and trigger the OOM killer, which terminates processes such as MySQL. Adding swap space on Linux lets the kernel move rarely used data to disk when RAM runs short, giving you a safety buffer. This guide shows how to create a swap file, enable it at boot and tune swappiness on KVM VPS and dedicated servers running Debian/Ubuntu or CentOS/Rocky/AlmaLinux.
Use free -h to see RAM and existing swap, swapon --show to see whether swap is already enabled, and df -h to make sure the root partition has enough free space. Typical sizes:
| RAM | Suggested swap |
|---|---|
| 1 GB or less | 1–2 GB |
| 2–4 GB | 2 GB |
| 8 GB or more | 2–4 GB (or as needed) |
free -h
swapon --show
df -h /
fallocate allocates a file of the requested size instantly. If it reports that the operation is not supported (on some older filesystems), use dd instead — slower but works everywhere. The examples create a 2 GB /swapfile.
# Create a 2 GB swap file
fallocate -l 2G /swapfile
# If fallocate fails ("Operation not supported"), use dd instead
dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
A swap file can contain sensitive data from memory, so it must be readable and writable by root only (600). Format it with mkswap, enable it immediately with swapon, and free -h should now show the swap size.
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
swapon --show
free -h
swapon only lasts until the next reboot. Add a line to fstab so swap is activated automatically. Back up fstab first.
cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' >> /etc/fstab
Swappiness (0–100) controls how eagerly the kernel uses swap. On servers you usually want RAM to be used first and swap only when necessary, so a value around 10 is a good choice. Put it in /etc/sysctl.d/ to make it permanent.
cat /proc/sys/vm/swappiness # usually 60 (Debian/Ubuntu) or 30 (some RHEL images)
sysctl vm.swappiness=10 # apply now
# Make it permanent
echo 'vm.swappiness=10' > /etc/sysctl.d/99-swappiness.conf
sysctl --system
If you no longer need swap or want to resize it, turn it off, delete the fstab line and remove the file. To resize, simply repeat steps 2–4.
swapoff /swapfile
sed -i '\|^/swapfile |d' /etc/fstab
rm -f /swapfile
free -h
"Invalid argument" usually means fallocate created the file on a filesystem that does not support it for swap — recreate it with dd. "Operation not permitted" typically means container-based virtualisation that does not allow custom swap; contact support to confirm.
No. A huge swap file does not improve performance and wastes disk space, and a system that swaps heavily becomes very slow. Follow the table above; if memory is genuinely short, upgrade the plan.
Run grep VmSwap /proc/*/status | sort -k2 -n | tail to list the biggest swap users, or install htop and enable the SWAP column.
Still stuck after following these steps? Open a support ticket and the IMIDC 24/7 technical team will help. Please include the server IP, operating system, the commands you ran and a screenshot of the error so we can pinpoint the issue faster.