Linux – How does top calculate the memory figures on Linux

linuxmemorytop

I'm a bit confused on how the memory figures add up in top.

If I've understood correctly, the amount of "really" used memory is totalfreebufferscached. In the example below that would be 14370248k – 75736k – 178892k – 10459552k = 3656068k, which is 25% of the total memory (3656068/14370248).

Again, if I've understood correctly, the numbers shown in RES and %MEM columns show the amount of physical memory a process "really" uses. The sum of the RES values of just httpd processes is 7254m, or 7428096k (7254*1024), which is 52% of the total memory (7428096/14370248). The sum of %MEM values of httpd processes is 53%, close enough.

How come the summary part shows just 25% of memory being used, while httpd processes alone claim to use over 50% of memory? What am I missing here?

top - 15:37:00 up 117 days, 18:27,  6 users,  load average: 4.82, 4.44, 3.60
Tasks: 433 total,   4 running, 429 sleeping,   0 stopped,   0 zombie
Cpu(s): 59.6%us,  6.5%sy,  0.0%ni, 33.0%id,  0.1%wa,  0.0%hi,  0.8%si,  0.0%st
Mem:  14370248k total, 14294512k used,    75736k free,   178892k buffers
Swap:  8388600k total,      116k used,  8388484k free, 10459552k cached

PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
3566 mysql     21   0  554m  60m 4576 S 74.6  0.4  11422:52 mysqld
23305 apache    15   0  327m  27m 3792 S  3.2  0.2   0:01.30 httpd
23474 apache    15   0  327m  27m 3800 S  3.2  0.2   0:01.09 httpd
...

Best Answer

The httpd processes probably share some memory, because they are forked from each other. Because of the copy-on-write mechanism, they all claim that memory as their own virtual memory even though it only exists once in physical memory.

In general, the memory output on Linux contains various bogosities of this kind. I wouldn't try to make too much sense of it. It is useful to see if a process is behaving differently from how it should; but the numbers don't necessarily add up.