Linux Swap Explained: What Happens When RAM Runs Out (and How to Fix It)
Every Linux server has a moment of truth: what happens when the RAM fills up? Unlike a laptop that freezes and dies, Linux has a backup plan called swap — disk space used as emergency memory. Swap has saved countless servers from crashing, and it has also silently destroyed performance on just as many. Here is how it actually works.
What Is Swap?
Swap is disk space reserved to act as overflow memory. When RAM fills up, the kernel moves pages of memory that are not being used right now — idle program data, cached files — out to the swap area on disk. That frees RAM for the programs that need it. If the swapped-out data is needed again, the kernel reads it back from disk.
Swap lives in one of two places: a dedicated swap partition (common on servers) or a swap file (easy to add later). You can see yours with one command:
swapon --show
free -h
The free -h output shows total RAM, used RAM, and how much swap exists and is in use. If swap is at zero on a busy server, either you have plenty of RAM — or you have no swap at all.
When Swap Saves You
Swap is a safety net. Without it, a memory spike — a runaway process, a sudden traffic burst — forces the kernel's Out-Of-Memory killer to start terminating processes. It may kill your database, your web server, or something critical, with no warning. With swap, the kernel gets breathing room: it can push idle memory to disk and keep everything alive while you investigate.
On a small VPS with 1–2 GB of RAM, even a modest swap file can be the difference between a slow night and a dead service.
When Swap Kills Performance
Disk is thousands of times slower than RAM. A server that is constantly swapping — reading and writing memory pages to disk in a loop — is thrashing, and it will feel like it is frozen. The load average climbs, commands take seconds, and nothing finishes. Swap is an emergency lane, not a second highway.
This is why "just add more swap" is bad advice for a server that is genuinely short on memory. Swap buys time; it does not buy capacity.
How to Tune It
The kernel's swappiness setting (0–100) controls how eagerly it moves memory to swap. The default of 60 is a reasonable middle ground, but servers have opinions:
# Check the current value
cat /proc/sys/vm/swappiness
# Make the kernel avoid swap unless truly necessary (good for databases)
sudo sysctl vm.swappiness=10
# Make the change permanent
echo "vm.swappiness=10" | sudo tee -a /etc/sysctl.conf
A low value like 10 keeps data in RAM as long as possible and only swaps when memory pressure is real. A high value like 80 swaps early and often — fine for a desktop, bad for a database server.
Adding a Swap File
No swap configured? Add one in a minute:
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
That creates 2 GB of swap and makes it survive reboots. Just remember to set the permissions to 600 — a world-readable swap file can leak memory contents.
The Security Angle
Swap can hold copies of sensitive data — passwords, keys, decrypted files — that were once in RAM. On a laptop, that means an attacker with physical access could read secrets from the swap file even after shutdown. The defense: encrypt your swap, or use swapoff and encrypted volumes on machines that leave your control.
The Bottom Line
Swap is not a performance feature and it is not a replacement for RAM. It is insurance: cheap, invisible when you do not need it, and the thing that keeps your server alive at 3 AM when a process misbehaves. Check your swappiness, make sure swap exists, and never let a server run with zero margin.



Comments
Post a Comment