Calculate percent of memory used in Linux

by Daniel Pham
Published: Updated:

How to calculate percent of memory used in Linux? This article will guide you to do that.

For Linux desktops, you can easily see the percentage of RAM used on your computer through programs like System Monitor. But for Linux servers, only the command line, you must use it.

Free command in Linux

Linux provides a free command to allow you to see how much memory is used and how much is available.

To see the memory status, type the command below.

free -mth
  • Option -m : show output in megabytes.
  • Option -t : show total for RAM + swap.
  • And option -h: show human-readable output.

Calculate percent of memory used

To calculate the percentage, it’s very simple, it’s just a mathematical formula.

Percentage = (memory used / total memory) * 100

how to calculate percent of memory used in Linux
How to calculate percent of memory used in Linux.

First, we will get the total memory information available on the system with the following command. KB unit.

TOTAL_MEM=`free | grep Mem | awk '{print $2}'`
  • grep Mem : print out Memory line from free command result.
  • awk '{print $2}' : use awk command to print out column number 2.

Next, we get the memory information used by the command below. KB unit.

USED_MEM=`free | grep Mem | awk '{print $3}'`
  • awk '{print $3}' : use awk command to print out column number 3.

And finally, we use the following command to calculate the percentage of memory used.

PERCENT=$(awk "BEGIN {printf \"%.2f\",($USED_MEM/$TOTAL_MEM)*100}")
  • $(awk "BEGIN {}") : calculation expression using awk command in bash.
  • printf \"%.2f\" : print out the results with 2 decimal places.
  • ($USED_MEM/$TOTAL_MEM)*100 : memory percentage expression.

Now the variable PERCENT already contains the percentage of memory used, you just need to use the command below to see the result.

echo $PERCENT

Conclusion

I wrote this article because there is a fact that I have experienced. My boss wants me to monitor Linux systems but doesn’t want me to use third party agent software.

So I had to get the system information myself and calculate it. And I wrote a completely bash script monitor program.

(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).

0 0 votes
Article Rating

You may also like

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.

SUPPORT US

FOLLOW US

Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 DevOpsLite.com – All rights reserved.

Please write sources “DevOpsLite.com” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

0
Would love your thoughts, please comment.x
()
x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.