When the physical memory or RAM on our system is full, we end up making use of the swap space on our systems. In this process, the inactive pages of our memory are moved to the swap space, creating more memory resources. This space is especially useful when a system is down on RAM; however, swap space is located on the hard drive and, hence, slower to access. Therefore, it should not be considered an appropriate alternative to RAM.
In this article, we will describe a few ways to check for available swap space on your Ubuntu system. The commands and procedures described in this article have been run on an Ubuntu 22.04 LTS system.
We are using the Ubuntu command line, the Terminal, to check swap space on our system. To open the Terminal, you can either use the Dash or the Ctrl+alt+T shortcut. Then, you can choose the following ways to get swap space information:
The Linux free command
Command:
$ free
This command checks memory and swap utilization on your system in a few lines. Without the use of any switch, the displayed output is printed in kilobytes.
Command:
$ free -h
With the -h switch, the free command displays the memory and swap utilization in nearest possible, 3 digit format.
The swapon command
Command:
$ swapon -s
You can use the swapon command to check swap on a particular partition, logical volume or a file. Here, we will use it with the -s (summary) switch in order to get swap details (in kilobytes).
The top command
Command:
$ top
The header section of the top command output shows the swap space information, in kilobytes. Other commands that give this information include htop, glances, and itop, etc.
The vmstat command
Command:
$ vmstat
Through the vmstat command, you can view the swap in and swap out information. However, you can not see the total values of the swap as seen is the previously mentioned commands.
The /proc/swaps file
Command:
$ cat /proc/swaps
You can also view the swap size information through the swap configuration file /proc/swaps. It also displays device-wise swap information so that you can see the device name (partition, logical volume, or file), its type, and how much swap it contributes to the system.
Through the straightforward commands described in this article, you can view the swap space on your Ubuntu system and utilize it when your system is running low on RAM resources.