如何计算已用内存?
在以下情况下,15784520(“top”命令的 RSS 内存)+ ? = 16031516(“free”命令的已用内存)
root@host:~# free -w
total used free shared buffers cache available
Mem: 82376116 16031516 30084440 16780 2328 36257832 65487292
Swap: 0 0 0
root@host:~# ps aux | awk '{sum +=$5}END{print sum}'
111284060
root@host:~# ps aux | awk '{sum +=$6}END{print sum}'
15784520
root@host:~# cat /proc/meminfo
MemTotal: 82376116 kB
MemFree: 30077632 kB
MemAvailable: 65480784 kB
Buffers: 2328 kB
Cached: 30545640 kB
SwapCached: 0 kB
Active: 24199852 kB
Inactive: 19097572 kB
Active(anon): 12741724 kB
Inactive(anon): 7588 kB
Active(file): 11458128 kB
Inactive(file): 19089984 kB
Unevictable: 160 kB
Mlocked: 160 kB
SwapTotal: 0 kB
SwapFree: 0 kB
Dirty: 312 kB
Writeback: 0 kB
AnonPages: 12741616 kB
Mapped: 1301004 kB
Shmem: 16780 kB
KReclaimable: 5712492 kB
Slab: 7411268 kB
SReclaimable: 5712492 kB
SUnreclaim: 1698776 kB
KernelStack: 40496 kB
PageTables: 51424 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 41188056 kB
Committed_AS: 26394284 kB
VmallocTotal: 34359738367 kB
VmallocUsed: 328328 kB
VmallocChunk: 0 kB
Percpu: 106656 kB
HardwareCorrupted: 0 kB
AnonHugePages: 4096 kB
ShmemHugePages: 0 kB
ShmemPmdMapped: 0 kB
FileHugePages: 0 kB
FilePmdMapped: 0 kB
CmaTotal: 0 kB
CmaFree: 0 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
Hugetlb: 0 kB
DirectMap4k: 3090304 kB
DirectMap2M: 73455616 kB
DirectMap1G: 9437184 kB
答案1
进程使用了 15.7 GB,总共使用了 16.0 GB。
这意味着内核“幕后”使用了大约 300 MB 的空间,仅仅是它自己使用或者用于文件系统操作。
这绝不是不自然的,这只是 Ubuntu/Linux(或任何其他操作系统)的工作方式。
另请参阅这个问题。