devutils/ATFQ/book ATFQ git

Part II · Your system

17.3 Swap and swappiness

alsoswapswappinesszramswap fileswapfileswapping

TL;DR

  • Swap is disk space the kernel uses as overflow for RAM: app memory that is rarely touched moves there.
  • A little swap in use is fine. Constant swapping in and out is what makes a machine slow.
  • zram is swap in compressed RAM: no disk involved. Fedora uses it by default.
swapon --show
vmstat 1        # si and so: swapped in and out, per second

On your machine

alice@laptopexample
$ atfq 17.3
  RAM       10.5 GiB available of 15.5 GiB (68%) · 1.1 GiB completely free
  Cache     7.5 GiB in buff/cache, 604.0 MiB of it shared (tmpfs)
  Swap      256.0 MiB used of 8.0 GiB · swappiness 60

An example: Alice's laptop, one of the test machines. In your terminal, this block shows your own machine, read live.

Pitfalls

Deep dive 2 questions

Why swap out when RAM is still free?

The kernel balances two uses of RAM: app memory and file cache. An app page nobody touched for hours is worth less than file data in active use, so moving it out makes the machine faster overall. vm.swappiness sets that balance (see 17.1).

Swap partition, swap file or zram?

A partition is the classic setup. A swap file performs as well on current kernels and is easier to resize. zram keeps compressed pages in RAM, often at about half their size: it suits laptops and small machines, and it costs some CPU instead of disk time. Some setups combine zram first and a disk swap behind it.

Commands
swapon vmstat
Paths
/proc/swaps /proc/sys/vm/swappiness
See also
17. Memory
17.1 free vs available
17.2 The OOM killer