Linux – Why does the memory usage in “top” not add up

linuxmemorytop

I have noticed that sometimes when I run top, the memory usage of each process in the process table does not seem to add up to the total.

For example, in the dump below, top says that I am using 16 Gb of memory. However, the process table only shows two processes using a little over 520 Mb. How can I find out what is consuming the other 15.5 Gb? (I'm using CentOS.)

$ top

top - 12:16:34 up 45 days,  2:28,  3 users,  load average: 0.24, 0.65, 0.71
Tasks: 274 total,   1 running, 273 sleeping,   0 stopped,   0 zombie
Cpu(s):  2.3%us,  0.2%sy,  0.0%ni, 97.5%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  16432032k total, 16340144k used,    91888k free,    21736k buffers
Swap: 18481144k total,     1112k used, 18480032k free, 15624488k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
18159 jsmith    15   0  260m  31m 4560 S 16.6  0.2  53:35.64 python
 4795 26        15   0  260m 6608 4220 S  2.0  0.0   0:00.06 postmaster
    1 root      15   0 10344  680  568 S  0.0  0.0   0:39.36 init
    2 root      RT  -5     0    0    0 S  0.0  0.0   0:00.53 migration/0
    3 root      34  19     0    0    0 S  0.0  0.0   0:00.62 ksoftirqd/0
    4 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/0
    5 root      RT  -5     0    0    0 S  0.0  0.0   0:02.09 migration/1
    6 root      34  19     0    0    0 S  0.0  0.0   0:01.32 ksoftirqd/1
    7 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/1
    8 root      RT  -5     0    0    0 S  0.0  0.0   0:00.99 migration/2
    9 root      34  19     0    0    0 S  0.0  0.0   0:01.74 ksoftirqd/2
   10 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/2
   11 root      RT  -5     0    0    0 S  0.0  0.0   0:02.16 migration/3
   12 root      34  19     0    0    0 S  0.0  0.0   0:01.30 ksoftirqd/3
   13 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/3
   14 root      RT  -5     0    0    0 S  0.0  0.0   0:01.94 migration/4
   15 root      34  19     0    0    0 S  0.0  0.0   0:01.78 ksoftirqd/4
   16 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/4
   17 root      RT  -5     0    0    0 S  0.0  0.0   0:01.92 migration/5
   18 root      34  19     0    0    0 S  0.0  0.0   0:01.30 ksoftirqd/5
   19 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/5
   20 root      RT  -5     0    0    0 S  0.0  0.0   0:02.06 migration/6
   21 root      34  19     0    0    0 S  0.0  0.0   0:01.83 ksoftirqd/6
   22 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/6
   23 root      RT  -5     0    0    0 S  0.0  0.0   0:02.31 migration/7
   24 root      34  19     0    0    0 S  0.0  0.0   0:01.50 ksoftirqd/7
   25 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/7
   26 root      10  -5     0    0    0 S  0.0  0.0   0:00.42 events/0
   27 root      10  -5     0    0    0 S  0.0  0.0   0:00.28 events/1
   28 root      10  -5     0    0    0 S  0.0  0.0   0:00.37 events/2
   29 root      10  -5     0    0    0 S  0.0  0.0   0:00.21 events/3
   30 root      10  -5     0    0    0 S  0.0  0.0   0:00.38 events/4
   31 root      10  -5     0    0    0 S  0.0  0.0   0:00.27 events/5
   32 root      10  -5     0    0    0 S  0.0  0.0   0:00.52 events/6
   33 root      10  -5     0    0    0 S  0.0  0.0   0:00.64 events/7
   34 root      10  -5     0    0    0 S  0.0  0.0   0:00.00 khelper

Best Answer

From the memory usage related lines in top:

Mem: 16432032k total, 16340144k used, 91888k free, 21736k buffers
Swap: 18481144k total, 1112k used, 18480032k free, 15624488k cached

Let's ignore the swap. Total memory equals the sum of used and free memory. Used, on the other hand, is the sum of "really used by applications" and cached and buffers. So, in your case goes like this:

  • Mem = 16432032k = 16340144k + 91888k;
  • "Really used by applications" = Used - (cached + buffers) = 16340144k - (15624488k + 21736k) = 693920k.

The other 15.5 GB are cached. This improves performance. However, in the very moment an application requires part of the cached memory it is immediately given to it. You will notice this if you run some memory hungry application and monitor top.