Skip to main content

Linux swap configuration — when to use it and how much

TL;DR

Guide to configuring swap on Ubuntu and Debian servers covering sizing rules, creation, swappiness tuning, and when swap helps or hurts performance.

Key facts

Topology
Swap on Ubuntu/Debian servers

TL;DR

Swap is disk space that acts as overflow memory. When physical RAM fills up, the kernel moves inactive pages to swap instead of killing processes outright. A server without swap risks OOM (Out of Memory) kills under transient load spikes — a 2 AM deploy that briefly doubles memory usage, a runaway query, or a traffic surge. This guide covers when swap helps, how much to allocate, how to configure it, and when it does more harm than good.

When you need swap

Swap is not a substitute for adequate RAM. It is a safety net. You need swap when:

  • Your server runs close to its memory ceiling during peak load
  • You run background jobs or scheduled tasks that spike memory usage unpredictably
  • The cost of an OOM kill (process death, data corruption, downtime) is higher than the cost of brief slowdown
  • You are on a small VPS (1-2 GB RAM) where a single process can exhaust available memory

Sizing rules

There is no universal formula. Use these guidelines:

Server RAMRecommended swap
1 GB1-2 GB
2 GB2 GB
4 GB2-4 GB
8 GB+2-4 GB (fixed)
32 GB+2 GB (safety only)

For servers with 8 GB or more, a fixed 2-4 GB swap provides OOM protection without wasting disk. Scaling swap linearly with RAM is unnecessary on modern servers.

Create a swap file

Most cloud providers and modern Ubuntu installations prefer swap files over swap partitions:

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

Verify it is active:

sudo swapon --show
free -h

Make it permanent

Add the swap file to /etc/fstab so it survives reboots:

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

Swappiness tuning

The vm.swappiness parameter controls how aggressively the kernel moves pages to swap. The default is 60, which is appropriate for desktops but too aggressive for servers:

# Check current value
cat /proc/sys/vm/swappiness

# Set to 10 for servers — prefer keeping data in RAM
sudo sysctl vm.swappiness=10

# Make permanent
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.d/99-swap.conf

A value of 10 means the kernel will only swap when RAM usage is very high, which is the right behaviour for web and application servers where response latency matters.

Monitoring swap usage

# Summary view
free -h

# Detailed swap info
sudo swapon --show

# Per-process swap usage (top consumers)
for pid in /proc/[0-9]*; do
    awk '/VmSwap/{printf "%s %s KB\n", FILENAME, $2}' "$pid/status" 2>/dev/null
done | sort -k2 -rn | head -10

# Watch swap in real time
vmstat 1 5

If your server is consistently using significant swap, you need more RAM — swap is buying you time, not solving the problem.

When swap is harmful

Swap is not always beneficial:

  • Database servers: MySQL and PostgreSQL perform poorly when their buffer pools are swapped to disk. Database servers should have enough RAM to hold their working set entirely in memory. Set vm.swappiness=1 or even 0 on dedicated database hosts
  • SSD wear: Swap on SSDs causes write amplification. On small SSDs with limited write endurance, heavy swap activity shortens the drive's lifespan. Monitor writes with iostat
  • Latency-sensitive applications: If your application cannot tolerate occasional 10-100x latency spikes on memory access, swap will cause problems under pressure

Removing swap

If you decide swap is not appropriate:

sudo swapoff /swapfile
sudo rm /swapfile
# Remove the /swapfile line from /etc/fstab
sudo sed -i '/swapfile/d' /etc/fstab

zswap and zram alternatives

For memory-constrained servers where disk-backed swap is too slow, zswap compresses pages in RAM before writing to disk, and zram creates a compressed block device in memory:

# Enable zswap (compressed swap cache)
echo 1 | sudo tee /sys/module/zswap/parameters/enabled

# Verify
grep -r . /sys/module/zswap/parameters/

zram is useful on very small VPS instances where you want swap-like OOM protection without disk I/O:

sudo apt install -y zram-tools
sudo systemctl enable zramswap
sudo systemctl start zramswap

With Reflex

Reflex monitors swap usage across your fleet and alerts you when a server starts swapping heavily — a leading indicator of memory pressure that often precedes OOM kills and degraded response times. See How it works.