swapon

@amitmund September 11, 2026

Linux swapon — Complete Learning Notes & Output Guide

swapon is a core system management utility used to enable, configure, and inspect swap space (swap partitions and swap files) in Linux, allowing the operating system to page out inactive memory pages and free up RAM for active processes.


1. What is swapon?

When a Linux system runs low on physical RAM, the kernel's memory management subsystem uses swap space as virtual memory overflow.

swapon answers fundamental memory management questions:

  • Which swap partitions or swap files are currently active and mounted on the system?
  • How much total swap capacity is currently allocated and utilized?
  • What is the priority ordering of active swap devices when the kernel needs to evict memory?
  • How do we dynamically bring a new swap file or swap partition online without rebooting?

2. Installation & Availability

swapon is a core utility bundled inside the util-linux package, pre-installed by default across every standard Linux distribution.

Verify the binary:

swapon --version


3. Basic Syntax

swapon [options] [swap_device_or_file]

To view all currently active swap devices and files:

swapon --show
# or
swapon -s


4. Anatomy of swapon --show Output

When you run swapon --show, the kernel queries active memory structures and prints a detailed breakdown of configured swap areas:

NAME           TYPE      SIZE   USED PRIO
/dev/sda2      partition   4G     0B   -2
/swapfile      file        2G 512.4M    0

Breakdown of Every Output Heading

Heading Example Technical Meaning
NAME /dev/sda2, /swapfile The device file path or filesystem file path assigned as swap space.
TYPE partition, file The underlying architectural type: a raw disk partition or a regular file allocated on a filesystem (file).
SIZE 4G, 2G The total physical capacity allocated to that swap space.
USED 0B, 512.4M The amount of swap space currently occupied by swapped-out memory pages.
PRIO -2, 0 The priority (priority) assigned to the swap area. The kernel allocates swap pages starting with the highest priority value first.

5. Anatomy of swapon -s (Legacy /proc/swaps View)

Running swapon -s (or reading /proc/swaps) presents a legacy tabulated view:

Filename                        Type        Size        Used    Priority
/dev/sda2                       partition   4194304     0       -2
/swapfile                       file        2097152     524800  0

  • Filename: Absolute path of the swap target.
  • Type: partition or file.
  • Size: Total size expressed in Kilobytes (KB) (e.g., $4,194,304\text{ KB} = 4\text{ GB}$).
  • Used: Active swap utilization in Kilobytes.
  • Priority: Integer swap priority ranging from -1 to 32767.

6. Essential Command Options & Cheat Sheet

Flag Description Practical Example
--show Print active swap spaces in a clean, readable table format. swapon --show
**-s, --summary** Print summary statistics (maps directly to /proc/swaps). swapon -s
**-a, --all** Enable all swap devices marked with auto in /etc/fstab. sudo swapon -a
**-p <prio>, --priority <prio>** Assign a specific priority (0 to 32767) to the swap space being enabled. sudo swapon -p 10 /swapfile
--show=... Output custom columns only (e.g., NAME,SIZE,USED). swapon --show=NAME,SIZE,USED

7. Workflow: Creating and Activating a New Swap File

When physical RAM is exhausted and you need to expand virtual memory without partitioning disks, you create a swap file:

# 1. Allocate a 4GB file using fallocate (fast) or dd
sudo fallocate -l 4G /swapfile

# 2. Restrict strict read/write permissions to root only (security critical)
sudo chmod 600 /swapfile

# 3. Format the file into swap space structures
sudo mkswap /swapfile

# 4. Activate the swap file immediately with priority 10
sudo swapon -p 10 /swapfile

# 5. Verify activation
swapon --show

To make the swap file persistent across reboots, append the following entry to /etc/fstab:

/swapfile none swap sw,pri=10 0 0


8. Deactivating Swap: swapoff

To disable an active swap space and migrate its paged contents back into physical RAM (if RAM is available), use swapoff:

# Disable a specific swap file
sudo swapoff /swapfile

# Disable all active swap spaces
sudo swapoff -a

(Note: If physical RAM is currently full, swapoff will block or fail until sufficient memory is freed up to pull paged data back off the swap device).


9. Related Kernel Tuning: vm.swappiness

While swapon manages swap space activation, the kernel parameter vm.swappiness controls how aggressively the kernel pages anonymous memory out to swap.

  • Range: 0 to 100.
  • Low values (1 to 10): Avoids swapping whenever possible, keeping processes in physical RAM (ideal for database servers where disk swap latency hurts query performance).
  • High values (60 to 100): Aggressively pages inactive memory to swap to maximize available cache memory.

Check current swappiness:

cat /proc/sys/vm/swappiness

Temporarily set swappiness to 10:

sudo sysctl vm.swappiness=10


10. Important Interview Questions & Answers

Q: What happens if multiple swap spaces are active, and how does the kernel choose which one to write to?

Answer: When multiple swap spaces are active, the kernel allocates pages based on their priority (PRIO). Swap spaces with higher priority numbers are filled first before the kernel falls back to lower priority areas. If two or more swap areas share the exact same priority value, the kernel utilizes them in a round-robin fashion to distribute I/O bandwidth across multiple disks or controllers.

Q: Why is a swap file on a modern NVMe SSD slower than a dedicated swap partition?

Answer: A raw swap partition maps directly to block device sectors, bypassing filesystem lookup overhead. In contrast, a swap file relies on the underlying filesystem (ext4, XFS). If the swap file becomes heavily fragmented, the kernel must traverse file extent maps or indirect blocks to locate physical sectors during paging, introducing additional metadata lookup latency compared to contiguous partition blocks.


0 Likes
2 Views
0 Comments

Filters

No filters available for this view.

Reset All