NewCode CLOUD — 20% off your first month on all cloud VPSClaim

How to Add a Swap File on Linux (and How Much Swap You Need)

When a Linux server runs out of memory, the kernel’s OOM killer picks a process and terminates it — often your database or PHP-FPM, at the busiest moment. Many VPS images ship with no swap at all, so a short spike from a backup, a composer install or a traffic burst is enough to crash a service.

A swap file gives the kernel somewhere to put memory pages that are not being actively used. It is not a replacement for RAM — disk is far slower — but it turns hard crashes into a temporary slowdown. This guide shows how to add one in a few minutes and how to size and tune it.

Check whether you already have swap

free -h
swapon --show

If the Swap: line in free -h shows 0B, there is none. swapon --show lists any active swap files or partitions.

Also check free disk space, since the swap file uses it:

df -h /

How much swap do you need?

The old “twice your RAM” rule dates from machines with 256 MB of memory. For servers today:

RAM Suggested swap Why
1–2 GB 1–2 GB Absorbs spikes on small VPS plans
4–8 GB 2 GB A safety buffer; you should rarely use it
16 GB and above 2–4 GB Mainly so the kernel can move idle pages out of RAM

If you need hibernation (servers almost never do), swap must be at least the size of RAM. Otherwise, more swap does not make a server faster — if it constantly uses gigabytes of swap, it needs more memory, not a bigger swap file.

Create a swap file

These steps work on Ubuntu, Debian, AlmaLinux and Rocky Linux with the default ext4 or XFS filesystems.

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
  • fallocate reserves 2 GB instantly. If it reports “Operation not supported”, use sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress instead.
  • chmod 600 matters: swap can contain fragments of passwords and keys, so only root may read it.

Confirm it is active:

swapon --show
free -h

Make it permanent

Without an entry in /etc/fstab, the swap file is forgotten at the next reboot:

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

A mistake in /etc/fstab can stop a server from booting, which is why the backup copy is worth making. Check the file before rebooting:

sudo findmnt --verify

Tune swappiness

Swappiness (0–100) controls how eagerly the kernel moves memory to swap. The default of 60 suits desktops; servers usually do better keeping applications in RAM and using swap only under pressure.

cat /proc/sys/vm/swappiness          # current value
sudo sysctl vm.swappiness=10         # change now
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swap.conf   # keep after reboot

A value of 10 is a good starting point for web and database servers. Do not set it to 0 unless you understand the consequences: on modern kernels it can make the OOM killer act sooner.

A related setting, vm.vfs_cache_pressure, controls how quickly the kernel drops cached directory and file information. The default (100) is fine for most servers; leave it unless you have measured a reason to change it.

Resize or remove the swap file

To change the size, switch it off, recreate it and switch it on again:

sudo swapoff /swapfile
sudo fallocate -l 4G /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

swapoff moves everything in swap back into RAM, so run it when there is enough free memory, or it can take a long time.

To remove swap entirely: sudo swapoff /swapfile, delete the line from /etc/fstab, then sudo rm /swapfile.

Alternative: zram (compressed swap in RAM)

zram creates a compressed block device in memory and uses it as swap. Because memory compresses well (often 2–3×), a 2 GB VPS can effectively hold more, and swapping to zram is much faster than to disk. It costs some CPU.

On Ubuntu and Debian:

sudo apt install zram-tools
echo -e "ALGO=zstd\nPERCENT=50" | sudo tee /etc/default/zramswap
sudo systemctl restart zramswap

On AlmaLinux / Rocky 9: sudo dnf install zram-generator and create /etc/systemd/zram-generator.conf with:

[zram0]
zram-size = ram / 2
compression-algorithm = zstd

You can use zram and a disk swap file together; the kernel prefers the one with the higher priority (zram is set higher by default).

Is swap hurting performance?

Some swap usage is normal: idle pages from services you rarely use sit there harmlessly. What matters is constant swapping in and out. Watch it with vmstat:

vmstat 2 10

Look at the si (swap in) and so (swap out) columns. Occasional small numbers are fine. Values that stay high while the site is slow mean the server is short of RAM.

Find which processes are in swap:

for f in /proc/[0-9]*/status; do awk '/^Name|^VmSwap/{printf "%s ", $2} END{print ""}' "$f"; done 2>/dev/null | sort -k2 -n | tail

When to add RAM instead

Swap is a cushion, not a solution. Consider a larger plan when:

  • si/so in vmstat are high during normal traffic, not just during backups.
  • Your database’s buffer pool or PHP-FPM workers cannot be sized sensibly within available RAM.
  • The OOM killer still appears in journalctl -k | grep -i oom after adding swap.

Before upgrading, check that one process is not leaking memory — restarting a misbehaving service is cheaper than more RAM. On Server.HK cloud VPS, you can upgrade memory from the client area when you outgrow the plan.

Checklist

  • free -h shows swap after a reboot
  • /swapfile has permissions 600
  • Entry added to /etc/fstab and verified with findmnt --verify
  • vm.swappiness set to around 10 for servers
  • vmstat checked under normal load — no sustained swapping