Linux6 min read

Understanding Linux swap space

linuxswapmemoryperformance

Swap is disk space used as an extension of RAM. When physical memory runs low, the kernel moves less-used pages to swap. It keeps the system running instead of killing processes, but at the cost of speed — disk is much slower than RAM.

How swap works

The Linux kernel manages memory in pages (usually 4KB each). When memory pressure increases, it identifies pages that haven’t been accessed recently and moves them to swap. If those pages are needed again, they’re paged back in. This is called swapping or paging.

The swappiness parameter controls how aggressively the kernel swaps:

cat /proc/sys/vm/swappiness

Default is usually 60. Lower values (like 10) tell the kernel to prefer keeping things in RAM and only swap when necessary. Higher values make it swap earlier.

Check current swappiness:

sysctl vm.swappiness

Change it temporarily:

sudo sysctl vm.swappiness=10

Make it permanent in /etc/sysctl.conf:

vm.swappiness=10

Checking swap usage

free -h

Shows total, used, and free swap alongside memory.

swapon --show

Shows which devices or files are being used for swap.

cat /proc/swaps

Same information in a different format.

Swap partition vs swap file

Traditionally, swap was a dedicated partition. Swap files are more flexible — you can create, resize, or remove them without repartitioning.

Creating a swap file

sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

fallocate creates the file instantly without actually writing zeros to disk. chmod 600 is important — swap files shouldn’t be readable by other users.

Add to /etc/fstab:

/swapfile none swap sw 0 0

Creating a swap partition

sudo fdisk /dev/sdb  # create a partition, type 82 (Linux swap)
sudo mkswap /dev/sdb1
sudo swapon /dev/sdb1

Add to /etc/fstab:

/dev/sdb1 none swap sw 0 0

How much swap do you need

For servers with plenty of RAM, swap is a safety net, not a primary resource. Common guidelines:

  • Servers with 1-4GB RAM: Swap equal to RAM
  • Servers with 4-16GB RAM: 2-4GB swap
  • Servers with 16GB+ RAM: 2GB swap (or less)
  • VMs and containers: Enough to handle memory spikes

If your server is regularly using significant swap, it needs more RAM, not more swap.

Disabling swap

sudo swapoff /swapfile

Or disable all swap:

sudo swapoff -a

To permanently remove a swap file, also remove the /etc/fstab entry and delete the file.

Swap and performance

Heavy swapping kills performance. A system that’s “swapping” (thrashing) spends more time moving pages between RAM and disk than doing actual work.

Signs of swap thrashing:

  • High wa (I/O wait) in top or vmstat
  • System feels sluggish despite low CPU usage
  • si and so columns in vmstat showing high numbers
vmstat 1 5

The si (swap in) and so (swap out) columns show pages swapped per second. If these are consistently high, you have a memory problem.

tmpfs and /dev/shm

/dev/shm is a tmpfs filesystem that lives in RAM. It counts toward memory usage but not swap. Some applications use it for shared memory.

df -h /dev/shm

Common mistakes

No swap at all. Running without swap means the OOM (Out of Memory) killer starts terminating processes when RAM runs out. A small swap gives the system breathing room.

Too much swap. 32GB of swap on an 8GB server just means the system grinds to a halt instead of failing fast. Better to let the OOM killer handle it than to have a server that’s unresponsive for hours.

Swap on SSD. Works fine but adds wear to the SSD. For servers, the performance benefit of SSD swap often outweighs the wear concern.

Forgetting /etc/fstab. Swap works after you create it, but after a reboot it’s gone unless you add it to fstab.

Remarks

Swap is a safety net, not a performance feature. Configure a reasonable amount, set swappiness low on servers, and monitor usage with free -h. If your server is regularly using swap, add more RAM. The goal is to have swap available but rarely used.

Related Posts