LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-27-2015, 12:40 AM   #1
aristosv
Member
 
Registered: Dec 2014
Posts: 125

Rep: Reputation: Disabled
correctly measure available memory in linux


I'm currently using the command 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?

Thanks
 
Old 03-27-2015, 01:06 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,848

Rep: Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823
That so-called command is in fact spawning six commands (in addition to the "free"). All unnecessary as a single awk call can do the lot.
How much of that "command" do you understand ?.

The free command does all the math for you - that's what the -/+ line is showing you. Do the math yourself using the buffers and cached values in the line you are parsing.
 
Old 03-27-2015, 01:26 AM   #3
aristosv
Member
 
Registered: Dec 2014
Posts: 125

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by syg00 View Post
All unnecessary as a single awk call can do the lot.
How?

Quote:
Originally Posted by syg00 View Post
How much of that "command" do you understand ?.
Very little, considering this is the Newbie forum.

Quote:
Originally Posted by syg00 View Post
Do the math yourself using the buffers and cached values in the line you are parsing.
No idea what you just said.

I can appreciate the attitude, since you're obviously an LQ veteran, and I'm a newbie. But at least point me to some examples, or guides. Don't just treat this like an RTFM case. Because if you do, then what's the point of these forums? Just make a website saying RFTM and point everyone there.
 
Old 03-27-2015, 01:34 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,848

Rep: Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823
You may have cobbled that "command" together yourself - in which case you know what you are looking at.
Or not if you merely lifted it off some web page somewhere.

We are not prescient.

Read this.
 
1 members found this post helpful.
Old 03-27-2015, 01:46 AM   #5
aristosv
Member
 
Registered: Dec 2014
Posts: 125

Original Poster
Rep: Reputation: Disabled
Please read my original post. If you can assist, then do so. If you cannot please don't reply any more.
 
Old 03-27-2015, 04:05 AM   #6
Head_on_a_Stick
Senior Member
 
Registered: Dec 2014
Location: London, England
Distribution: Arch & Debian
Posts: 1,183

Rep: Reputation: 283Reputation: 283Reputation: 283
Read this:
http://www.linuxatemyram.com/
EDIT: Sorry syg00! Didn't check your link...

I always use `ps_mem` to guage memory usage.
https://github.com/pixelb/ps_mem/

Last edited by Head_on_a_Stick; 03-27-2015 at 04:06 AM.
 
Old 03-27-2015, 08:04 AM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,110
Blog Entries: 10

Rep: Reputation: 1525Reputation: 1525Reputation: 1525Reputation: 1525Reputation: 1525Reputation: 1525Reputation: 1525Reputation: 1525Reputation: 1525Reputation: 1525Reputation: 1525
Why aren't you looking at the file /proc/meminfo? That is after all the file which the free command uses to produce it's output.
 
Old 03-27-2015, 08:59 AM   #8
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181
Just correcting your misunderstanding of free, but leaving your questionable use of awk and bash (because I'm not competent in those myself):
Code:
memusage=$(memusage=$(/usr/bin/free | grep -e-/+ | awk '{print $3/($3+$4) * 100.0 ""}' | cut -d "" -f1);echo "$(printf "0.0f\n" $memusage)") && echo Memory Usage is $memusage
That is based on the simplified assumption that the numbers free puts on the -/+ line are meaningful and those on the Mem line are confusing (for the LinuxAteMyRam reasons).

That simplified assumption tends to be true for casual users of Linux. But it is not necessarily true. Believing the free column on the Mem line is certainly unsound and typically gives you a value way below the "truth". But in less common cases the free column on the -/+ line is also distorted and gives an answer way above or below the "truth".

Last edited by johnsfine; 03-27-2015 at 09:05 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
tool to measure cpu/memory usage by a process saritha Linux - Software 1 10-08-2007 07:16 AM
Why is memory load hard to measure? MasterOfTheWind Linux - General 4 06-01-2007 06:27 AM
How to _accurately_ measure memory usage? rhimbo Linux - Kernel 10 02-21-2007 11:00 PM
How to measure a program's peak memory consumption? hae Linux - Software 2 06-23-2005 09:39 AM
measure memory usage unosoft Linux - Software 1 10-07-2003 07:25 PM


All times are GMT -5. The time now is 07:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration