Printable Version of Topic

Click here to view this topic in its original format

Linuxhelp _ Programming in Linux _ correctly measure available memory in linux

Posted by: aristosv Mar 27 2015, 02:15 AM

I'm currently using the command(s) below, to output the memory usage of a debian system in bash scripts.

CODE
memusage=$(memusage=$(/usr/bin/free | grep Mem | awk '{print $3/$2 * 100.0 "%"}' | cut -d "%" -f1);echo "$(printf "%0.0f\n" $memusage)") && echo Memory Usage is $memusage


Being new to Linux though, I just found out that Linux takes up a lof of memory and puts it in cached.

CODE
root@Client1:~# free -m
             total       used       free     shared    buffers     cached
Mem:           927        799        128          0         15        754
-/+ buffers/cache:         28        898
Swap:           99          0         99


So the first command will actually output that the memory usage is 87%, when in reality is not. Its actually thinking that the memory usage is high because its also counting the cached memory, as used memory.

How can I modify the first command not to consider cached memory, as used memory?

Posted by: michaelk Mar 27 2015, 05:42 AM

Free RAM in your case is the 898

root@Client1:~# free -m
total used free shared buffers cached
Mem: 927 799 128 0 15 754
-/+ buffers/cache: 28 898 <----
Swap: 99 0 99

So the percentage is (898/927)*100

Posted by: aristosv Mar 27 2015, 05:57 AM

Thanks for your reply. Please note that the command I'm using has been put together using various bits and pieces found online. I don't completely comprehend how it works.
I understand that it greps the first "Mem" line and puts into variables the third and second value. Then divides the values and multiplies by 100.

How can I make a variable for the 898 number?

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)