Developers

Linux Swap Explained: What It Is and How to Add a Swap File Safely

Swap lets Linux move rarely used memory to disk. Learn when it helps, when it hurts, how to create a swap file and how to tune swappiness on a small VPS.

By Zebra Hosting Cloud

If your small VPS occasionally kills its database with a mysterious "out of memory" message, swap may be the missing piece. Swap is disk space that Linux uses as an overflow for memory. It is widely misunderstood: some people think it is useless on modern servers, others treat it as free RAM. The truth sits in between, and this article explains what swap does, when it is worth having and how to set it up properly.

What swap does and why a small server benefits

What swap actually does

RAM is fast but limited; disk is slow but plentiful. When memory runs low, the kernel can move pages that have not been used recently out to a swap area on disk, freeing RAM for active work. If those pages are needed again, they are read back in. Swapping out memory that nobody is using is helpful. The kernel also uses spare RAM for the file cache, so the "free" figure on a healthy Linux system is usually small and not a cause for worry.

Why a small server benefits

Without swap, once memory runs out the kernel has no choice but to invoke the out-of-memory (OOM) killer, which ends a process, often the biggest one, which is frequently your database. With a modest amount of swap, a short spike can be absorbed by moving idle pages to disk, and the server carries on. Swap also lets the system push out seldom-used memory such as long-idle daemons, leaving more RAM for caching.

Swap does not make a server fast. Disk is far slower than memory, so if the working set of your applications genuinely does not fit in RAM, the machine will constantly shuffle pages back and forth. This "thrashing" makes everything crawl. The right reading is: swap is a safety net, not extra memory. If it is used heavily all the time, you need more RAM or a lighter configuration.

Check and create a swap file

Check what you have

free -h
swapon --show
cat /proc/sys/vm/swappiness

The first shows memory and swap totals. The second lists active swap areas; no output means there is none. The third prints swappiness, the kernel's tendency to swap, which defaults to 60 on most distributions.

Create a swap file

A swap file is easier to resize than a partition and works well on a VPS. This example creates 2 GB; choose a size to suit your server:

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
swapon --show

The chmod 600 step is important: swap can contain fragments of memory, including secrets, so only root should read it. To keep it after a reboot, add it to /etc/fstab:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Use that line only once, and check /etc/fstab afterwards, as a broken entry can slow boot. On filesystems where fallocate does not create a usable swap file, such as some btrfs and ZFS setups, follow the documentation for that filesystem, or create the file with dd instead. Some container-based VPS technologies do not let you manage swap from inside the guest at all; if swapon reports an error, ask your provider how memory and swap are configured.

Size and tuning

How much swap?

There is no universal formula. Older advice of "twice the RAM" dates from small-memory machines. For a small VPS, a swap area between half of RAM and equal to RAM is a common, reasonable choice, and larger servers need relatively less. Remember that more swap only delays the point where a runaway process is killed, and a heavily swapping server is slow. Think of it as insurance against spikes, not a plan for daily operation.

Tune swappiness

The vm.swappiness setting ranges from 0 to 100 and controls how readily the kernel swaps out pages instead of dropping file cache. For database and application servers, many administrators lower it to around 10, so the kernel prefers to keep application memory in RAM and uses swap mostly under pressure. To set it persistently:

echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
sudo sysctl --system

This is a tuning preference, not a magic cure. Test on your workload, and watch activity with vmstat 5: sustained non-zero values in the si and so columns (swap in and out) indicate real pressure.

Remove or resize swap

To remove a swap file, turn it off, delete the line from /etc/fstab and then delete the file:

sudo swapoff /swapfile
sudo rm /swapfile

To resize, do the same and create a new file of the desired size. Make sure there is enough free RAM to take back whatever was swapped out before you run swapoff.

Swap on a server with a database

Databases keep hot data in memory deliberately, so you should avoid a setup where the database's buffers are pushed to swap, because queries then become unpredictably slow. Size the database's memory settings below what the machine can truly spare, leave swap as a buffer for spikes, and check vmstat occasionally. Our knowledge base has more on checking memory use.

Swap and your hosting

A well-sized server needs little swap, which is why choosing the right plan matters more than any tuning. If you often see swap in heavy use, compare options on our VPS page.