Without swap, a memory spike can OOM-kill processes. A 1–2 GB swap file on a 1–2 GB RAM VPS is usually a sensible cushion.
Steps
Check whether swap already exists:
free -hswapon --showCreate a 2G file, format and enable it:
sudo fallocate -l 2G /swapfilesudo chmod 600 /swapfilesudo mkswap /swapfilesudo swapon /swapfilefree -hMake it persistent in fstab:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstabsudo swapon --showOptional: lower swappiness:
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.confsudo sysctl -p /etc/sysctl.d/99-swappiness.conf
Tips
- If
fallocatefails on some filesystems, usesudo dd if=/dev/zero of=/swapfile bs=1M count=2048. - Swap is not a RAM upgrade: if the VPS is thrashing, resize the plan.
ext step
More real RAM when swap is not enough: Deploy your Cloud VPS.