Linux VPS Out of Memory? How to Add a Swap File

Your application runs fine for days, then the database disappears from the process list and the logs say Out of memory: Killed process. That is the Linux OOM killer doing its job: when RAM runs out and there is nowhere else to put memory pages, the kernel kills the largest process. A swap file gives the kernel somewhere else to go. It does not make a small server fast, but it turns a crash into a slowdown you can see and fix.
Tested on a real server: we ran every command in this guide on an Ubuntu 24.04 test VPS with 2 GB of RAM.
What swap is, and what it is not
Swap is disk space the kernel uses as overflow for memory pages that are not being used right now. On NVMe storage it is far slower than RAM but much faster than older disks. Use it as a safety net for short spikes, such as a build, a backup job or a traffic burst. If your server uses swap heavily all day, the real fix is more RAM, not more swap.
Step 1: Check what you have
free -h
swapon --showIf swapon --show prints nothing, the server has no swap. Also check free disk space with df -h / before you continue.
Step 2: Choose a size
As a rule of thumb:
1 to 2 GB RAM: a swap file equal to your RAM.
4 to 8 GB RAM: 2 to 4 GB of swap is usually enough.
16 GB RAM and above: 4 GB as a safety margin, unless your software documents a different need.
Step 3: Create and enable the swap file
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfileThe chmod 600 step matters: swap can contain fragments of memory, including secrets, so only root should be able to read it. If fallocate is not supported on your filesystem, use sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 instead.
Step 4: Make it permanent
Without this step the swap file is gone after the next reboot.
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstabRun sudo swapon --show again to confirm it is active.
Step 5: Tune swappiness
The vm.swappiness setting controls how eagerly the kernel moves pages to swap. Many distributions default to 60. For a server where you want RAM used first and swap kept for emergencies, a lower value is a common choice:
sudo sysctl vm.swappiness=10
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.confStep 6: Verify with htop
htopThe Swp bar in htop now reads 0K/2.00G: the swap file is active and ready, and nothing is using it yet because RAM still has room.
All the commands in one place
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.confHow to tell whether swap is hiding a bigger problem
Watch it over time:
vmstat 5shows thesiandsocolumns. Occasional activity is fine. Constant swapping in and out means the server is short of memory.Find the memory user:
ps aux --sort=-%mem | headlists the largest processes.Cap the heavy services: database caches, PHP-FPM worker counts and Node.js heap size can usually be set lower so they fit the server.
Frequently asked questions
How much swap should I add?
There is no single right number. Start with the sizes in Step 2, then watch usage: if swap fills up regularly, the server needs more RAM, not a bigger swap file.
Does swap make my server faster?
No. Swap prevents processes from being killed when RAM runs out. Heavy swapping makes a server slower, because disk is much slower than memory.
How do I remove the swap file?
Run sudo swapoff /swapfile, then sudo rm /swapfile, and delete the /swapfile line from /etc/fstab.
How do I know if the server is using swap?
Check the used column on the Swap line in free -h, or the Swp bar in htop.
When to upgrade instead
If swap is busy most of the day, response times will suffer no matter how you tune it. On an IM HOST Linux VPS you get full root to set up swap exactly as above, with NVMe storage, a dedicated IPv4 plus an IPv6 /64, and DDoS protection, from $5/month. When the workload outgrows that, Cloud VPS plans start at 4 dedicated vCPU and 8 GB RAM and can be upgraded anytime.
More from our blog
Discover more practical guides and product insights from the IM Host team.
View all articles