5

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?

Prakash
  • 203
  • 3
  • 8
  • 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 – Prakash Jan 24 '13 at 06:33

1 Answers1

5

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.

David Schwartz
  • 31,215
  • 2
  • 53
  • 82
  • 1
    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}'` – Prakash Jan 24 '13 at 09:57
  • 2
    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