Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm trying to calculate the amount of memory used by AMP in an LAMP stack machine.

top -bn1 | grep -E '(mysql|httpd|php)' | awk '{mem += $(NF-2)} END {print mem}'

But the sum generated using the above command is greater than 100%, but I was expecting something below 100% since individual processes memory usage is already represented in % by top.

Please help me understand whether memory reported by top can't be used this way to calculate the memory usage?

share|improve this question
2  
Adding up percentages is almost always the wrong way to do things. –  Zoredache Jan 23 '13 at 17:21
    
@Zoredache Yeah! I totally agree that. It would be helpful if you could let me know any other method is find the memory usage in percentage of various processes –  pprakash Jan 24 '13 at 6:33

1 Answer 1

up vote 2 down vote accepted

The %MEM field is the number of pages of physical memory the process is using times 100 divided by the total number of pages of physical memory. There is no reason it should sum to 100 -- consider ten processes that are all using the very same page of physical memory.

You may also have a buggy version of top that computes %MEM as VSZ/mem instead of RSS/mem. Find a case where these produce different results and make sure the top value is RSS/mem.

Use top -v to see if you have the BusyBox version of top. That tends to be buggier than the procps version.

share|improve this answer
    
Thanks for your inputs. I'm now taking the total system memory using the following command. Please let me know whether this is a right approach free | grep 'buffers/cache' | awk '{printf "%.2f", (($NF-1)/($NF + $(NF-1)))*100}' –  pprakash Jan 24 '13 at 9:57
1  
What are you expecting this measurement to tell you? It will roughly tell you the percentage of physical memory used for things other than buffers and caches. –  David Schwartz Jan 24 '13 at 17:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.