Help - Search - Members - Calendar
Full Version: correctly measure available memory in linux
Linuxhelp > Support > Programming in Linux
I'm currently using the command(s) below, to output the memory usage of a debian system in bash scripts.

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.

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?
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
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?
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2017 Invision Power Services, Inc.