Check swap memory in Linux
You can check swap the same way you check memory usage in Linux: using the free command.
If you use the free command with human readable output (with option -h), it will show you the RAM and Swap uses.
For my system, it shows the following output:
total used free shared buff/cache available Mem: 7.5G 5.8G 365M 726M 1.4G 787M Swap: 14G 1.1G 13G
As you can see in the above output, my system has 14 GB of total swap space and out of that 1.1 GB is being used. Around 13 GB of swap space is free.
There are more ways to check swap usage in Linux and you may get some additional information about swap as well.
Other ways to check swap usage in Linux
My favorite way to check swap usage in Linux is by using the swapon command. This is a command dedicated for handling swap memory. Apart from giving accurate information about the swap space being used, it also tells you if the swap space is a partition or a swap file.
You can use it like this:
As you can see in the output, the total swap space is 14.9 GB (not 14 GB as free command showed). 1.1 GB is being used and it’s a swap partition, not a swap file.
NAME TYPE SIZE USED PRIO /dev/nvme0n1p4 partition 14.9G 1.1G -2
Using swap file is a better idea because you easily increase swap size on Linux with it. Changing swap partition is not a good idea.
Another way to get swap memory details is by using the /proc/meminfo file. The proc directory in Linux filesystem hierarchy stores system usage information for running processes.
cat /proc/meminfo | grep -i swap
Here’s the output:
SwapCached: 164116 kB SwapTotal: 15625212 kB SwapFree: 13714756 kB
You can also check swap usage in Linux with top, htop or any other system monitoring tool.
Bonus Tip: Find out which process is using swap space in Linux
You can use an open source utility smem to get more accurate memory usage in Linux. smem is not installed by default so you will have to install it using your distribution’s package manager.
In Ubuntu, smem is available in the universe repository. You can install it using this command:
sudo apt install smem
Once installed, you can use smem to check which process is using how much swap on your system.
You can simply use smem command in the terminal but if your focus is to check swap usage, I suggest reverse sorting it on swap in the following manner:
smem -rs swap
It will show all the running process in reverse order of swap utilization:
PID User Command Swap USS PSS RSS 3931 abhishek /opt/google/chrome/chrome 147668 296852 300926 341716 7483 abhishek /opt/google/chrome/chrome - 141524 392900 398545 464700 15774 abhishek /opt/google/chrome/chrome - 127256 454080 456262 525048 1400 abhishek Telegram -- 122060 20528 22514 24832 3964 abhishek /opt/google/chrome/chrome - 108436 101632 111780 138312 2774 abhishek /usr/bin/gnome-shell 101936 204676 210229 225780 9170 abhishek /opt/google/chrome/chrome - 71620 68184 70255 121364 473 abhishek /opt/google/chrome/chrome - 57768 265880 267665 334252 8733 abhishek /opt/google/chrome/chrome - 52236 47280 48685 104332 9422 abhishek /opt/google/chrome/chrome - 41520 62708 64566 120084
The memory utilization is in kb.
Well, I hope you now know how to check swap memory usage in Linux. You may also want to learn about clearing swap on Linux.
If you are using swap file, you can learn to increase swap size in this video on our YouTube channel:
If you have questions or suggestions, please let me know in the comments below.