Linux free shows high memory usage but top does no

2019-03-07 19:39发布

问题:

On RedHat Linux 6.2 I'm running free -m and it shows nearly all 8GB used

             total       used       free     shared    buffers     cached
Mem:          7989       7734        254          0         28       7128
-/+ buffers/cache:        578       7411
Swap:         4150          0       4150

But at the same time in top -M I cannot see any processes using all this memory:

top - 16:03:34 up  4:10,  2 users,  load average: 0.08, 0.04, 0.01
Tasks: 169 total,   1 running, 163 sleeping,   5 stopped,   0 zombie
Cpu(s):  0.7%us,  0.3%sy,  0.0%ni, 98.6%id,  0.4%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  7989.539M total, 7721.570M used,  267.969M free,   28.633M buffers
Swap: 4150.992M total,    0.000k used, 4150.992M free, 7115.312M cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 1863 sroot     20   0  398m  24m 9.8m S  0.3  0.3   3:12.87 App1
    1 sroot     20   0  2864 1392 1180 S  0.0  0.0   0:00.91 init
    2 sroot     20   0     0    0    0 S  0.0  0.0   0:00.00 kthreadd
    3 sroot     RT   0     0    0    0 S  0.0  0.0   0:00.07 migration/0
    4 sroot     20   0     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/0
    5 sroot     RT   0     0    0    0 S  0.0  0.0   0:00.00 migration/0
    6 sroot     RT   0     0    0    0 S  0.0  0.0   0:00.00 watchdog/0
    7 sroot     RT   0     0    0    0 S  0.0  0.0   0:00.08 migration/1
    8 sroot     RT   0     0    0    0 S  0.0  0.0   0:00.00 migration/1

I also tried this ps mem script but it onlt shows about 400MB memory being used.

回答1:

Don't look at the "Mem" line, look at the one below it.

The Linux kernel consumes as much memory as it can to provide the I/O cache (and other non-critical buffers, but the cache is going to be most of this usage). This memory is relinquished to processes when they request it. The "-/+ buffers/cache" line is showing you the adjusted values after the I/O cache is accounted for, that is, the amount of memory used by processes and the amount available to processes (in this case, 578MB used and 7411MB free).

The difference of used memory between the "Mem" and "-/+ buffers/cache" line shows you how much is in use by the kernel for the purposes of caching: 7734MB - 578MB = 7156MB in the I/O cache. If processes need this memory, the kernel will simply shrink the size of the I/O cache.



回答2:

Also, as the first line shows total used free shared buffers cached Mem: 7989 7734 254 0 28 7128 -/+ buffers/cache: 578 7411

If we add (cached[7128] + buffers[28] + free[254]), we will get approximately the second line's free[7411] value 7128 + 28 + 254 = 7410