swapon
Activate swap space (paging) on Linux
TLDR
Show swap information
Enable a given swap area
Enable all swap areas specified in /etc/fstab except those with the noauto option
Enable a swap partition by its label
SYNOPSIS
swapon [options] specialfile...
swapon -a [options]
swapon -s
PARAMETERS
-a, --all
Activates all swap areas marked with "sw" in /etc/fstab. This is the most common way to enable swap at boot.
-d, --discard
Discards contents of the swap area before using it. Useful for SSDs to issue TRIM commands, potentially improving performance and lifespan.
-e, --ifup
Exits quietly if the specified specialfile does not exist.
-p, --priority
Specifies the priority of the swap area. Higher numbers mean higher priority. Swap areas with higher priority are used before those with lower priority.
-s, --summary
Displays a concise summary of swap usage by device, similar to the free command's swap line.
--show
Displays a more detailed, column-formatted summary of swap usage, including priority and type.
-v, --verbose
Increases verbosity, showing more details about the actions being performed.
DESCRIPTION
The swapon command is used to enable devices or files for paging and swapping in Linux. It makes a specified swap area available to the system, expanding the effective virtual memory beyond the physical RAM. When the system runs out of physical memory, less frequently used data from RAM is moved to the swap space, allowing applications to continue running, albeit with a performance penalty. Swap areas can be dedicated disk partitions or regular files.
Typically, swap spaces are configured to activate automatically during system boot by listing them in the /etc/fstab file. The swapon command is crucial for managing the system's memory resources, especially in scenarios where applications demand more memory than physically available. It works in conjunction with swapoff, which disables swap areas.
CAVEATS
While swap space is essential for system stability when RAM is scarce, it's significantly slower than physical memory. Excessive swapping can lead to poor system performance, often referred to as "thrashing."
Activating swap requires root privileges. Ensure that the designated swap area is correctly prepared (e.g., using mkswap) before enabling it. Using the --discard option on traditional HDDs offers no benefit and might even slightly decrease performance due to the overhead of issuing unnecessary TRIM commands.
SWAP PRIORITY
Linux assigns priorities to swap areas. When multiple swap areas are active, the system prefers using higher-priority areas first. If two or more areas have the same priority, they are used in a round-robin fashion. Priorities can be set using the -p option or in /etc/fstab.
PERMANENT SWAP CONFIGURATION
To make a swap area active automatically at boot, it must be listed in the /etc/fstab file. The typical entry for a swap partition looks like: /dev/sda2 none swap sw 0 0. For a swap file: /swapfile none swap sw 0 0. The swapon -a command then processes these entries during startup.
HISTORY
The swapon command has been a fundamental utility in Linux since its early days, part of the standard util-linux package. Its core functionality of enabling swap areas has remained consistent, reflecting the stable nature of Linux's virtual memory management. Over time, options like --discard have been added to adapt to modern storage technologies like SSDs, enhancing its utility in contemporary computing environments.