Skip to content
linux system-management

Swap

swap memory linux performance oom
Plain English

Swap is spare memory borrowed from the disk. When the computer runs low on RAM, it moves things you are not using right now out to swap to make room. That keeps programs from crashing, but disk is far slower than RAM, so a system that swaps heavily feels sluggish. A little swap use is normal; constant swapping means you need more RAM or fewer running programs.

Technical Definition

Swap is a swap partition or swap file the kernel uses to page out anonymous memory (memory not backed by a file on disk).

How Linux decides: the kernel balances reclaiming page cache against swapping process memory. vm.swappiness (0 to 200, default 60) biases that choice; lower values favor keeping process memory in RAM.

Swap file vs partition: Ubuntu creates /swap.img, a swap file, by default. Files are easier to resize; partitions were traditional and are still used with some setups.

Hibernation writes RAM to swap, so swap must be at least the size of RAM to hibernate.

zram / zswap: compressed swap in RAM. Fedora uses zram by default; it helps small-memory machines.

Signs of trouble:

  • si/so columns in vmstat consistently above zero: active swapping
  • High load average with low CPU usage and a busy disk
  • If RAM and swap are both exhausted, the OOM killer ends a process

In VMs and containers: many Kubernetes setups disable swap; hypervisor hosts generally avoid overcommitting memory into swap.

Source: Linux kernel documentation: Memory Management

Checking and managing swap

# Current swap devices and usage
$ swapon --show
NAME      TYPE SIZE USED PRIO
/swap.img file   4G   0B   -2

$ free -h

# Is the system actively swapping? (watch si and so)
$ vmstat 1 5

# Create an extra 2 GB swap file
$ sudo fallocate -l 2G /swapfile2 && sudo chmod 600 /swapfile2
$ sudo mkswap /swapfile2 && sudo swapon /swapfile2

# Lower swappiness persistently
$ echo "vm.swappiness=10" | sudo tee /etc/sysctl.d/99-swappiness.conf
$ sudo sysctl --system
In the Wild

Swap is a safety net, not extra RAM. A server with some swap used and nothing swapping in or out is healthy. A server with vmstat showing steady si and so is out of memory and slow, and the fix is more RAM, a smaller workload, or fixing a leak. For a learner VM with 4 GB of RAM, keep Ubuntu’s default swap file and give the VM more memory before tuning swappiness.