What swap is for
Swap is disk space the kernel uses when memory is full. It is far slower than RAM, so it is a safety net and not a substitute for memory. On a small VPS it can stop a package upgrade or a database from being killed during a short spike. If a server swaps constantly, it needs a bigger plan or a lighter workload.
Many images have no swap at all. Check before you add any, because a second swap area only wastes disk.
free -h
swapon --show
df -h /Choose a size
A common starting point for small servers is 1 to 2 GB, or the same as RAM on a 1 GB plan. Do not use more than you are willing to wait for: a process that has to run from swap is slow. Make sure the filesystem has enough free space, because the file is allocated immediately.
Create the swap file
These commands create a 2 GB file owned by root and readable only by root, format it as swap and turn it on. The dd command writes the file in full, which works on every filesystem including XFS on AlmaLinux. Change count to adjust the size in megabytes.
Never overwrite an existing /swapfile. If the image already provides swap, inspect it before adding more. Choose a separate unused path if an additional file is justified.
if sudo test -e /swapfile; then
echo '/swapfile already exists; inspect swapon --show and choose a different unused path.'
else
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
swapon --show
free -h
fiMake it permanent
Add a line to /etc/fstab so the file is enabled at boot. Back the file up first, and append the line only once. A mistake in fstab can stop a server from booting normally, so verify with the final command before you reboot.
sudo cp /etc/fstab /etc/fstab.bak
sudo grep -qE '^/swapfile[[:space:]]' /etc/fstab || echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
sudo findmnt --verify --fstabsudo reboot
# after logging in again:
swapon --showTune swappiness
The vm.swappiness setting controls how readily the kernel moves idle memory to swap. The default is 60. A lower value, such as 10, keeps more in RAM and uses swap mainly under pressure, which suits most servers. Put it in a file under /etc/sysctl.d/ so it survives reboots.
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
sudo sysctl --system | grep swappiness
cat /proc/sys/vm/swappinessNAME TYPE SIZE USED PRIO
/swapfile file 8G 0B -2
/swapfile.glws-validation file 2G 0B -3
vm.swappiness = 10
NAME TYPE SIZE USED PRIO
/swapfile file 8G 0B -2Check whether the server is under memory pressure
Free memory is not the best measure, because Linux uses spare memory for caching. Watch the available column and the si and so columns of vmstat, which show swap-in and swap-out. Occasional use is normal. Constant swapping, or out-of-memory messages in the kernel log, means the workload does not fit.
vmstat 5 5
sudo journalctl -k --since "1 day ago" | grep -i "out of memory"Remove the swap file
Turn the file off, delete its fstab line and remove the file. Use these steps if you upgrade the plan and no longer need it.
sudo swapoff /swapfile
sudo sed -i '\|^/swapfile |d' /etc/fstab
sudo rm /swapfile
swapon --showOfficial documentation
These upstream references provide more detail on the commands and workflows in this guide.