Rain Wiki · Linux

Swap space on Linux

What swap is, how to see it with free and swapon, how to add a swap file, and what vm.swappiness changes.

Linux · Rain Wiki

Swap is disk space that the Linux kernel uses as an overflow for memory: rarely used memory pages are moved there so RAM stays free for active work. It can be a dedicated partition or a swap file, and it is shown by the commands free -h and swapon --show.

Why swap is still useful

Even with plenty of RAM, swap lets the kernel move idle pages out of memory and gives a buffer before the out-of-memory killer stops processes. Heavy, constant swapping, however, means the machine needs more RAM or fewer workloads.

Swappiness

The kernel setting vm.swappiness (0-200, default 60) influences how readily the kernel swaps compared with dropping file cache. Lower values keep application memory in RAM longer. It is read with 'sysctl vm.swappiness' and set permanently in a file under /etc/sysctl.d/.

Swap files

A swap file is created, formatted with mkswap, enabled with swapon and added to /etc/fstab to survive reboots. Its permissions must be 600.

How to add a 2 GB swap file

  1. Create the file: sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
  2. Restrict access: sudo chmod 600 /swapfile
  3. Format it: sudo mkswap /swapfile
  4. Enable it: sudo swapon /swapfile
  5. Make it permanent: add the line '/swapfile none swap sw 0 0' to /etc/fstab
  6. Check: swapon --show and free -h

Cautions

  • On Btrfs a swap file needs special preparation (no copy-on-write); follow your distribution's guide.
  • A mistake in /etc/fstab can stop the system from booting: check the line before rebooting.

Sources

Last updated: 2026-10-08

Related articles