“top”和“free”之间的内存使用不一致

有什么我不明白的方式topfree报告内存使用情况。 如果我运行top,并从%MEM列中总结内存使用情况,那么我得到的内存使用量要比free报告低得多,我不明白为什么!

下面是freetop的输出。

 root@zamerli:~# free total used free shared buffers cached Mem: 329036 314412 14624 0 7820 97708 -/+ buffers/cache: 208884 120152 Swap: 524280 2660 521620 root@zamerli:~# top -b -n 1 top - 20:12:45 up 2:19, 1 user, load average: 0.00, 0.00, 0.00 Tasks: 103 total, 1 running, 102 sleeping, 0 stopped, 0 zombie Cpu(s): 1.1%us, 0.4%sy, 0.0%ni, 98.3%id, 0.3%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 329036k total, 314636k used, 14400k free, 7820k buffers Swap: 524280k total, 2660k used, 521620k free, 97708k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1 root 20 0 19320 1292 976 S 0 0.4 0:00.41 init 2 root 15 -5 0 0 0 S 0 0.0 0:00.00 kthreadd 3 root RT -5 0 0 0 S 0 0.0 0:00.00 migration/0 4 root 15 -5 0 0 0 S 0 0.0 0:00.01 ksoftirqd/0 5 root RT -5 0 0 0 S 0 0.0 0:00.00 watchdog/0 6 root 15 -5 0 0 0 S 0 0.0 0:00.00 events/0 7 root 15 -5 0 0 0 S 0 0.0 0:00.00 cpuset 8 root 15 -5 0 0 0 S 0 0.0 0:00.00 khelper 9 root 15 -5 0 0 0 S 0 0.0 0:00.00 async/mgr 10 root 15 -5 0 0 0 S 0 0.0 0:00.00 xenwatch 11 root 15 -5 0 0 0 S 0 0.0 0:00.00 xenbus 13 root RT -5 0 0 0 S 0 0.0 0:00.00 migration/1 14 root 15 -5 0 0 0 S 0 0.0 0:00.00 ksoftirqd/1 15 root RT -5 0 0 0 S 0 0.0 0:00.00 watchdog/1 16 root 15 -5 0 0 0 S 0 0.0 0:00.00 events/1 17 root RT -5 0 0 0 S 0 0.0 0:00.00 migration/2 18 root 15 -5 0 0 0 S 0 0.0 0:00.00 ksoftirqd/2 19 root RT -5 0 0 0 S 0 0.0 0:00.00 watchdog/2 20 root 15 -5 0 0 0 S 0 0.0 0:00.00 events/2 21 root RT -5 0 0 0 S 0 0.0 0:00.00 migration/3 22 root 15 -5 0 0 0 S 0 0.0 0:00.00 ksoftirqd/3 23 root RT -5 0 0 0 S 0 0.0 0:00.00 watchdog/3 24 root 15 -5 0 0 0 S 0 0.0 0:00.00 events/3 25 root 15 -5 0 0 0 S 0 0.0 0:00.00 kintegrityd/0 26 root 15 -5 0 0 0 S 0 0.0 0:00.00 kintegrityd/1 27 root 15 -5 0 0 0 S 0 0.0 0:00.00 kintegrityd/2 28 root 15 -5 0 0 0 S 0 0.0 0:00.00 kintegrityd/3 29 root 15 -5 0 0 0 S 0 0.0 0:00.00 kblockd/0 30 root 15 -5 0 0 0 S 0 0.0 0:00.00 kblockd/1 31 root 15 -5 0 0 0 S 0 0.0 0:00.00 kblockd/2 32 root 15 -5 0 0 0 S 0 0.0 0:00.00 kblockd/3 33 root 15 -5 0 0 0 S 0 0.0 0:00.00 kseriod 34 root 20 0 0 0 0 S 0 0.0 0:00.00 khungtaskd 35 root 20 0 0 0 0 S 0 0.0 0:00.00 pdflush 36 root 20 0 0 0 0 S 0 0.0 0:00.15 pdflush 37 root 15 -5 0 0 0 S 0 0.0 0:00.25 kswapd0 38 root 15 -5 0 0 0 S 0 0.0 0:00.00 aio/0 39 root 15 -5 0 0 0 S 0 0.0 0:00.00 aio/1 40 root 15 -5 0 0 0 S 0 0.0 0:00.00 aio/2 41 root 15 -5 0 0 0 S 0 0.0 0:00.00 aio/3 42 root 15 -5 0 0 0 S 0 0.0 0:00.00 jfsIO 43 root 15 -5 0 0 0 S 0 0.0 0:00.00 jfsCommit 44 root 15 -5 0 0 0 S 0 0.0 0:00.00 jfsCommit 45 root 15 -5 0 0 0 S 0 0.0 0:00.00 jfsCommit 46 root 15 -5 0 0 0 S 0 0.0 0:00.00 jfsCommit 47 root 15 -5 0 0 0 S 0 0.0 0:00.00 jfsSync 48 root 15 -5 0 0 0 S 0 0.0 0:00.00 xfs_mru_cache 49 root 15 -5 0 0 0 S 0 0.0 0:00.00 xfslogd/0 50 root 15 -5 0 0 0 S 0 0.0 0:00.00 xfslogd/1 51 root 15 -5 0 0 0 S 0 0.0 0:00.00 xfslogd/2 52 root 15 -5 0 0 0 S 0 0.0 0:00.00 xfslogd/3 53 root 15 -5 0 0 0 S 0 0.0 0:00.00 xfsdatad/0 54 root 15 -5 0 0 0 S 0 0.0 0:00.00 xfsdatad/1 55 root 15 -5 0 0 0 S 0 0.0 0:00.00 xfsdatad/2 56 root 15 -5 0 0 0 S 0 0.0 0:00.00 xfsdatad/3 57 root 15 -5 0 0 0 S 0 0.0 0:00.00 xfsconvertd/0 58 root 15 -5 0 0 0 S 0 0.0 0:00.00 xfsconvertd/1 59 root 15 -5 0 0 0 S 0 0.0 0:00.00 xfsconvertd/2 60 root 15 -5 0 0 0 S 0 0.0 0:00.00 xfsconvertd/3 61 root 15 -5 0 0 0 S 0 0.0 0:00.00 glock_workqueue 62 root 15 -5 0 0 0 S 0 0.0 0:00.00 glock_workqueue 63 root 15 -5 0 0 0 S 0 0.0 0:00.00 glock_workqueue 64 root 15 -5 0 0 0 S 0 0.0 0:00.00 glock_workqueue 65 root 15 -5 0 0 0 S 0 0.0 0:00.00 delete_workqueu 66 root 15 -5 0 0 0 S 0 0.0 0:00.00 delete_workqueu 67 root 15 -5 0 0 0 S 0 0.0 0:00.00 delete_workqueu 68 root 15 -5 0 0 0 S 0 0.0 0:00.00 delete_workqueu 69 root 15 -5 0 0 0 S 0 0.0 0:00.00 kslowd 70 root 15 -5 0 0 0 S 0 0.0 0:00.00 kslowd 71 root 15 -5 0 0 0 S 0 0.0 0:00.00 crypto/0 72 root 15 -5 0 0 0 S 0 0.0 0:00.00 crypto/1 73 root 15 -5 0 0 0 S 0 0.0 0:00.00 crypto/2 74 root 15 -5 0 0 0 S 0 0.0 0:00.00 crypto/3 77 root 15 -5 0 0 0 S 0 0.0 0:00.00 net_accel/0 78 root 15 -5 0 0 0 S 0 0.0 0:00.00 net_accel/1 79 root 15 -5 0 0 0 S 0 0.0 0:00.00 net_accel/2 80 root 15 -5 0 0 0 S 0 0.0 0:00.00 net_accel/3 81 root 15 -5 0 0 0 S 0 0.0 0:00.00 sfc_netfront/0 82 root 15 -5 0 0 0 S 0 0.0 0:00.00 sfc_netfront/1 83 root 15 -5 0 0 0 S 0 0.0 0:00.00 sfc_netfront/2 84 root 15 -5 0 0 0 S 0 0.0 0:00.00 sfc_netfront/3 310 root 15 -5 0 0 0 S 0 0.0 0:00.00 kstriped 315 root 15 -5 0 0 0 S 0 0.0 0:00.00 ksnapd 1688 root 15 -5 0 0 0 S 0 0.0 0:00.16 kjournald 1781 root 20 0 12900 712 520 S 0 0.2 0:00.07 upstart-udev-br 1783 root 16 -4 17196 428 292 S 0 0.1 0:00.06 udevd 1954 root 18 -2 17192 432 300 S 0 0.1 0:00.00 udevd 1955 root 18 -2 17192 172 168 S 0 0.1 0:00.00 udevd 2098 root 20 0 8192 448 444 S 0 0.1 0:00.04 dd 2104 syslog 20 0 180m 1268 904 S 0 0.4 0:00.01 rsyslogd 2252 root 20 0 18708 964 748 S 0 0.3 0:00.01 cron 2277 root 20 0 49072 1100 584 S 0 0.3 0:00.00 sshd 2282 root 20 0 5988 548 472 S 0 0.2 0:00.00 getty 8523 root 20 0 4004 604 492 S 0 0.2 0:00.00 mysqld_safe 8631 mysql 20 0 162m 29m 7092 S 0 9.2 0:00.25 mysqld 8632 root 20 0 5176 764 644 S 0 0.2 0:00.00 logger 10725 root 20 0 28580 1612 868 S 0 0.5 0:00.00 nginx 10784 root 20 0 107m 33m 3104 S 0 10.6 0:01.14 merb 10785 root 20 0 135m 38m 3116 S 0 11.9 0:00.88 merb 10790 www-data 20 0 28580 2208 824 S 0 0.7 0:00.01 nginx 11022 root 20 0 70440 3232 2488 S 0 1.0 0:00.02 sshd 11030 root 20 0 19100 2128 1568 S 0 0.6 0:00.02 bash 11121 root 20 0 19128 1188 876 R 0 0.4 0:00.01 top 

那么进程不包括内核本身使用的内存,它的caching等%MEM也不包括共享内存,比如共享库。 所以我认为这可能是没有什么可担心的。

至于顶级和自由报道的总数,他们基本上是一样的,从顶级14400,免费从14624。 这可能只是两个命令报告中的时间差异。

在“free”的输出中,你应该首先看看“ – / + buffers / cache”这行; 那行的用过的列将更加贴切地反映你从顶部join不同的“RES”行所得到的结果。

但也有“共享”内存。 顶部的“SHR”列反映了与其他进程可以共享的内存。 用技术术语来说,主要是“mmap”ed文件,但实际上这意味着大多数库。 当多个进程使用相同的共享库时,库代码只会在内存中加载一次。 但是,不能保证共享库实际上是由另一个程序加载的。 当一个stream程分叉时,也可以有相当数量的分享。 stream程可以共享内存还有一些其他的方式,但是它们的数量可能更小。

也许我误解了这个问题

(为清晰起见编辑)

 # free total used free shared buffers cached Mem: 329036 314412 14624 0 7820 97708 # top -b -n 1 Mem: 329036k total, 314636k used, 14400k free, 7820k buffers 

使用小于200k的微小差异可能很容易成为程序如何计算内存块的差异。 在命令之间的几秒钟甚至可能只是一个过程的差异。

编辑:对不起,我看到你在做什么。 最高内存使用率没有考虑caching内存,或者至less不包括某些发行版中的文件系统级高速caching内存。 我的猜测是大约60%左右的顶部显示使用的权利?