Skip to main content
In this lesson we cover swap space on Linux: what it is, when to use it, and how to create, enable, verify, and persist swap devices. Swap provides a safety buffer when physical RAM is exhausted by moving inactive memory pages to disk. It improves stability under heavy memory pressure but is much slower than RAM, so it is not a performance substitute for adding physical memory.
An infographic showing a RAM module and a disk cylinder connected by a dotted arrow to illustrate swap space. The caption explains that swap space is disk space used as a substitute for physical memory.
Key workflow for swap management:
  • Prepare the swap area with mkswap (on a partition or file).
  • Activate it immediately with swapon.
  • Make it persistent across reboots by adding an entry to /etc/fstab.
Swap is useful for system stability, but it is not a replacement for adequate RAM. Disk-backed swap is orders of magnitude slower than RAM.

Why use swap?

  • Prevents out-of-memory (OOM) conditions by giving the kernel somewhere to relocate inactive pages.
  • Useful for systems with variable memory loads or for hibernation (swap file/partition must be large enough).
  • Note: excessive swapping (thrashing) will degrade performance.

Demo — create two swap partitions, initialize, enable, and persist

This demo shows creating two swap partitions on a spare disk (/dev/sdb), initializing them with mkswap, enabling with swapon, verifying usage, and making them persistent by adding entries to /etc/fstab.
A dark-themed presentation slide titled "Demo — Managing Swap Space." It lists four steps: Create swap partition/file, Initialize using mkswap, Activate with swapon, and Verify swap usage.
  1. Inspect block devices and create two partitions on /dev/sdb using parted:
  1. Verify the newly created partitions and initialize them as swap with mkswap:
  1. Activate a swap device and verify active swap with swapon --show and free -h:
Notes:
  • swapon --show lists active swap areas and their priorities.
  • free -h shows total swap available to the system (existing swap + newly added).

Make swap persistent across reboots

To persist newly created swap devices, add entries to /etc/fstab. You can refer to swap partitions by UUID (recommended) or by device path. Below is an example fstab with the two swap devices added using absolute paths and priority values so the kernel prefers one swap area over another:
After saving /etc/fstab, enable all swap entries defined there:

Disable swap temporarily

For maintenance or testing you can turn off all swap with swapoff -a:

Quick reference — common swap commands

Checklist / Summary

  • You can create swap on a dedicated partition or a file.
  • Use mkswap to format the partition or file for swap usage.
  • Use swapon to enable swap immediately and swapon --show / free -h to verify.
  • Add swap entries to /etc/fstab to make them persistent across reboots.
  • Avoid relying on swap for performance — consider adding RAM if swapping is frequent.

Watch Video