the KodeLab

What Is Linux Swap and How to Add It on Debian/Ubuntu

775 words 4 min read
What Is Linux Swap and How to Add It on Debian/Ubuntu

What Is Swap?

Swap is disk space the Linux kernel uses when physical RAM is running low — the kernel moves rarely-used pages out of memory and onto disk to free up RAM for active workloads. Swap can live on a dedicated disk, a dedicated partition on the boot disk, or as a regular file inside any existing partition.

When you let the installer of a distro like Ubuntu or CentOS auto-partition your disk, it usually carves out a swap partition roughly the size of your RAM. Cloud VMs are different — Azure and Google Cloud Platform (GCP) images typically ship with no swap configured. You can still add it manually, which is what the second half of this article walks through.

Pros of using swap

The OS exists to manage hardware resources: handing out CPU time to processes, handing out memory to applications. When apps keep asking for more memory than is available, the kernel first reclaims unimportant pages — caches, buffers — to make room. If that still isn’t enough, the OOM killer steps in and terminates a process so the rest of the system stays alive.

Swap gives the kernel another option before that happens. Instead of killing a process outright, it can page cold data out to disk, buying the system more headroom and reducing the chance of a hard crash.

Cons of using swap

Swap lives on disk, so its latency and throughput are nowhere near RAM. Even a fast NVMe SSD topping out around 5 GB/s is dwarfed by DDR4-3200 memory bandwidth of roughly 25.6 GB/s — and that ignores the multi-order-of-magnitude latency gap.

If only a handful of cold pages get paged out, you won’t notice. But once the system starts swapping heavily, everything crawls — this is the classic “swap thrashing” scenario.

That’s why production servers often run without swap. Operators would rather have the service crash and auto-restart (via systemd or a process supervisor) than serve users a painfully slow experience. A restart also wipes the bloated memory state and shows up clearly in logs, prompting the operator to investigate whether more RAM is needed.

For personal use, swap is still worth it. An app crash mid-edit can lose your work; a slow system at least gives you time to save. But if you have plenty of RAM to begin with, you probably don’t need swap at all.

Adding Swap on Debian/Ubuntu

Check current swap status

sudo swapon --show

If swap isn’t configured, this command prints nothing. You can also run free — the Swap row will read 0 across the board.

Create the swap file

# Reserve a 1 GB file
sudo fallocate -l 1G /swapfile

# Restrict permissions to root only
sudo chmod 600 /swapfile

# Format it as swap space
sudo mkswap /swapfile
# Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
# no label, UUID=b3b91233-6a5b-44d3-9d13-d7c66285a166

fallocate reserves disk space in one go — adjust 1G to whatever size you need. Run df afterwards and you’ll see free disk space drop by 1 GB. mkswap prints a UUID; yours will differ.

Enable swap

sudo swapon /swapfile

Run sudo swapon --show again and the swapfile will appear. free will also show non-zero values on the Swap row.

sudo swapon --show
# NAME      TYPE  SIZE  USED PRIO
# /swapfile file 1024M 20.3M   -2
Output of free after enabling swap
Output of free after enabling swap

Enable swap automatically at boot

swapon doesn’t survive a reboot — only the /swapfile reservation does. To bring it back automatically, add an entry to /etc/fstab:

# Back up before editing a critical file
sudo cp /etc/fstab /etc/fstab.backup

# Edit fstab
sudo nano /etc/fstab
# Append this line:
/swapfile swap swap defaults 0 0

Tuning Swap Aggressiveness

The kernel exposes a swappiness parameter (0–100) that controls how eagerly Linux moves pages to swap. Higher values mean swap kicks in sooner. Lowering it tells the kernel to keep pages in RAM as long as possible and only swap when truly necessary.

# Check the current value (default: 60)
cat /proc/sys/vm/swappiness

# Set it to 10 — takes effect immediately
sudo sysctl vm.swappiness=10

# Persist across reboots (back up first)
sudo nano /etc/sysctl.conf
# Append this line:
vm.swappiness=10

Removing Swap

To undo everything, reverse the install order: stop swap, remove the fstab entry, then delete the file.

# Stop using swap
sudo swapoff -v /swapfile

# Remove the swap config
sudo nano /etc/fstab
# Delete this line:
/swapfile swap swap defaults 0 0

# Remove the reserved file
sudo rm /swapfile