Knowing how to check available swap space is crucial for managing memory on Linux systems. Whether you're troubleshooting performance issues or planning to optimize resource usage, Linux provides several simple commands to monitor swap space.
In this guide, we’ll explore easy ways to check your system's swap usage on both servers and desktops, to check the available swap space on your Linux server, you can use a few different commands. Here are the most common ones:
1. Using the free
Command
The free
command provides a summary of the system's memory usage, including swap space.
Run the following command:
free -h
- The
-h
flag makes the output human-readable (shows the values in GB, MB, etc.). - In the output, look for the Swap row, which will show the total, used, and free swap space.
Example output:
total used free shared buff/cache available
Mem: 7.7G 2.1G 1.2G 0B 4.4G 5.0G
Swap: 4.0G 0.0G 4.0G
In this example, the system has 4.0 GB of swap space, and none of it is being used (used 0.0G
).
2. Using the swapon
Command
The swapon
command can be used to display detailed information about swap devices and files that are currently in use.
Run this command:
swapon --show
Example output:
NAME TYPE SIZE USED PRIO
/swapfile file 4.G B -2
This shows the swap device or file (e.g., /swapfile
), its size, the used space, and its priority.
3. Using the vmstat
Command
The vmstat
command gives a snapshot of memory and swap usage. You can check swap space using:
vmstat
In the output, look at the si
(swap in) and so
(swap out) columns to see swap usage:
procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 0 4182644 167016 339928 0 0 0 3 999 153 5 1 94 0 0
In this example, there is no swap usage, as swpd
(swap used) is 0
.
4. Checking Swap Space Using /proc/meminfo
You can also check the swap space by looking at the contents of /proc/meminfo
:
cat /proc/meminfo | grep Swap
This will output information about the swap space, such as:
SwapTotal: 4194300 kB
SwapFree: 4194300 kB
- SwapTotal is the total amount of swap space available.
- SwapFree is the amount of swap space that is currently unused.
Summary of Commands:
free -h
: Provides a quick overview of memory and swap usage.swapon --show
: Displays detailed information about swap devices and files.vmstat
: Displays memory and swap usage, along with other system stats.cat /proc/meminfo | grep Swap
: Gives detailed swap statistics from/proc/meminfo
.
Using these commands, you can easily check the available swap space and monitor its usage on your Linux server.