Display the Memory and Swap Space Being Used
Tagged with cinnamon, linux on January 3, 2025
There are applets, desklets, screenlets, and widgets (and other things, depending on the Linux distribution) that can be used to display how much memory and swap space is being used. The command, “free -h”, will display what you need to see in the terminal, but what if you want to display it elsewhere?
As I alluded to in my article about Cinnamon Applets, Desklets, and Extensions, I like being able to see this information on the panel with the Cinnamon desktop environment. The “Command Runner” applet lets me display the results of shell scripts.
Display the Memory Used
A simple shell script can extract the information from the results of “free” command. The unit has an “i” in it that I remove from the retrieved display. This is the script:
free -h | awk 'NR == 2 { print substr( $3, 1, length($3)-1 ) }'
I can’t go into the details of using “awk” because I barely understand them myself.
Display the Swap Space Used
Again, a simple shell script can extract the information from the results of “free” command. The unit has an “i” in it that I remove from the retrieved display. This is the script:
free -h | awk 'NR == 3 { print substr( $3, 1, length($3)-1 ) }'
And again, I can’t go into the details of using “awk” because I barely understand them myself.
The swap usage displays all the swap space being used, which can be any combination of a swap file, swap partition, or zram (memory) partition. It adds them all together.
The Free Command
As I write this, the “free -h” (“h” for human-readable) command displays this in my terminal:
total used free shared buff/cache available Mem: 7.2Gi 2.3Gi 521Mi 70Mi 4.8Gi 4.9Gi Swap: 15Gi 16Ki 15Gi
On the right side of the panel, the corresponding information is displayed in three separate applets. The first applet displays the disk space being used, as I wrote about in my previous article. This is how it looks, except that the colors on the panel are white on black:
D: 95.74G M: 2.3G S: 16K
On this laptop computer, I have under 500 gigabytes available with the “ext4” file system, even though the raw space is actually 512 gigabytes. The total memory installed is eight gigabytes, and the total swap space allocated is 16 gigabytes. I made the swap space double that of the memory, just in case I want to hibernate the system instead of suspending it or turning it off.
Image by [email protected] Larry Ewing and The GIMP, CC0, via Wikimedia Commons
← Previous Article