Skip to main content
In this lesson we’ll explore how to configure and manage swap space in Linux. Swap is a dedicated area on disk where the kernel moves inactive pages from RAM when physical memory is exhausted. We’ll cover both swap partitions and swap files, with step-by-step examples.

Understanding Swap with a Simple Scenario

Imagine a system with 4 GiB of RAM:
  • A video editor consumes 2 GiB.
  • An audio editor consumes another 2 GiB.
At this point, RAM is full. With a 2 GiB swap partition, Linux can relocate the idle video editor’s memory pages to swap, freeing up 2 GiB of RAM for a new application such as Chrome.
The image illustrates the concept of managing swap space, showing a 4GB RAM with 2GB used by an audio editor and Chrome, and a 2GB swap partition used by a video editor.
Swap space is much slower than RAM. Use it as overflow memory, not as a substitute for adequate physical RAM.

Checking Existing Swap

List active swap areas:
Inspect block devices and partitions:
Example output:
In this example, /dev/vdb3 is reserved for swap but not yet active.

Command Reference

Creating and Enabling a Swap Partition

  1. Format the partition:
  2. Activate it immediately:
  3. Verify it’s active:
  4. Make it persistent by adding to /etc/fstab:

Disabling Swap

To turn off a swap partition or file:

Creating and Using a Swap File

If you cannot repartition the disk, create a swap file instead:
  1. Create a zero-filled file (2 GiB):
  2. Restrict permissions:
  3. Format the file as swap:
  4. Enable the swap file:
  5. Persist across reboots by adding to /etc/fstab:
Excessive swapping (thrashing) can severely degrade performance. Monitor swap usage with free -h, vmstat, or htop.
Practice these commands to master swap space management on Linux.

Watch Video