When the physical memory or RAM on our system is full, we end to make 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 18.04 LTS system.
We are making use of the Ubuntu command line, the Terminal, in order to check swap space on our system. In order 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
This command is used to check memory and swap utilization on your system in a few lines. Without the use of any switch, the displayed output is printed in kilobytes.
$ free -h
With the -h switch, the free command displays the memory and swap utilization in nearest possible, 3 digit format.
The swapon 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
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
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
$ cat /proc/swaps
You can also view the swap size information throough he swap configuration file /proc/swaps. It also displays device-wise swap information so that you can see device name (partition, logical volume or file), its type and how much swap it contributing to the system.
Though the very simple 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.