swapon
swapon
The swapon
command in Linux is used to activate swap space on the system. Here’s a detailed explanation:
Purpose:
swapon
is used to enable and activate swap partitions or swap files on a Linux system. Swap space serves as virtual memory, allowing the system to use disk space when physical RAM is fully utilized.
Usage:
Syntax:
swapon [options] device
Example:
swapon /dev/sda2
Options:
-a
: Activate all swap devices listed in/etc/fstab
.-v
: Verbose mode, providing detailed output about the activation process.
Device Specification:
Device: Typically, you specify a device file representing a swap partition (e.g.,
/dev/sda2
) or a swap file (e.g.,/swapfile
).Swap File Creation: If using a swap file, it needs to be created and initialized with
dd
andmkswap
commands before usingswapon
.
Considerations:
Performance: Activating swap (
swapon
) helps alleviate memory pressure and prevents out-of-memory situations by utilizing disk space as additional virtual memory.Monitoring: Use tools like
free
,top
, orvmstat
to monitor memory and swap usage.Permanent Activation: Modify
/etc/fstab
to ensure swap devices or files are activated automatically at system boot.
Managing Swap Configuration:
Temporary Activation: Use
swapon
command to activate swap space temporarily during runtime.Permanent Configuration: Update
/etc/fstab
to include entries for swap devices or files for automatic activation at boot time.
Best Practices:
Ensure sufficient swap space relative to physical RAM for optimal system performance.
Regularly monitor and adjust swap usage based on system workload and memory demands.
Conclusion
Understanding swapon
is essential for Linux administrators and users managing system resources effectively. It provides flexibility in utilizing swap space to improve system performance and manage memory efficiently. Always consider system requirements and workload demands when configuring and monitoring swap usage on Linux systems.
help
Usage:
swapon [options] [<spec>]
Enable devices and files for paging and swapping.
Options:
-a, --all enable all swaps from /etc/fstab
-d, --discard[=<policy>] enable swap discards, if supported by device
-e, --ifexists silently skip devices that do not exist
-f, --fixpgsz reinitialize the swap space if necessary
-o, --options <list> comma-separated list of swap options
-p, --priority <prio> specify the priority of the swap device
-s, --summary display summary about used swap devices (DEPRECATED)
--show[=<columns>] display summary in definable table
--noheadings don't print table heading (with --show)
--raw use the raw output format (with --show)
--bytes display swap size in bytes in --show output
-v, --verbose verbose mode
-h, --help display this help
-V, --version display version
The <spec> parameter:
-L <label> synonym for LABEL=<label>
-U <uuid> synonym for UUID=<uuid>
LABEL=<label> specifies device by swap area label
UUID=<uuid> specifies device by swap area UUID
PARTLABEL=<label> specifies device by partition label
PARTUUID=<uuid> specifies device by partition UUID
<device> name of device to be used
<file> name of file to be used
Available discard policy types (for --discard):
once : only single-time area discards are issued
pages : freed pages are discarded before they are reused
If no policy is selected, both discard types are enabled (default).
Available output columns:
NAME device file or partition path
TYPE type of the device
SIZE size of the swap area
USED bytes in use
PRIO swap priority
UUID swap uuid
LABEL swap label
breakdown
Last updated