Intro
常用命令free
free -h
以更易读(human-readable)的格式输出
free -s 3
每3秒输出一次
示例
wuyujin1997@budy:~$ free -htotal used freeshared buff/cache availableMem: 985M 253M 124M 2.7M 607M 566MSwap: 0B0B0Bwuyujin1997@budy:~$
行列意义
先区分行:
Mem
真正的物理内存Swap
交换空间(物理量内存过小时利用了硬盘空间进行“中转”的空间)
再区分列:
total
机器的整体内存大小(物理内存 + 处于硬盘区的交换空间)
used
已使用的内存大小,计算方式:total - free - buffers - cache
free
未使用/空闲的内存
shared
共享内存(tmpfs使用的内存)
buff/cache
内核使用的缓冲块buffer、页面缓存cache。
available
估算在不使用交换空间的前提下,有多少内存可用于新启动的程序运行。
表格分析
total used freeshared buff/cache availableMem: 985M 253M 124M 2.7M 607M 566MSwap: 0B0B0B
由表格可见,我的机器:
内存大小total
为:985M
已使用内存used
为:253M
空闲内存free
为:124M
buff/cache
之和为607M
而:已使用内存 + 空闲内存 +buff/cache
= 253 + 124 + 607 = 984M 约等于整体内存985M
两个核心指标:
total
机器整体内存有985M
available
新运行程序的可用内存大小为566M
(而非free的124M
)
available是大于free值的,因为如果新启动的程序内存不够用,可以去回收buff/cache
的大小来运行新的程序。
可得available
的值上限:free
+buff/cache
即:0 < available < (free + buff/cache)
毕竟buff/cache
使用的内存是不能被全部回收的。
程序文档
查看free命令的使用手册man free
NAMEfree - Display amount of free and used memory in the systemSYNOPSISfree [options]DESCRIPTIONfree displays the total amount of free and used physical and swap memory in the system, as well as the buffersand caches used by the kernel. The information is gathered by parsing /proc/meminfo. The displayed columns are:total Total installed memory (MemTotal and SwapTotal in /proc/meminfo)used Used memory (calculated as total - free - buffers - cache)free Unused memory (MemFree and SwapFree in /proc/meminfo)shared Memory used (mostly) by tmpfs (Shmem in /proc/meminfo)buffersMemory used by kernel buffers (Buffers in /proc/meminfo)cache Memory used by the page cache and slabs (Cached and SReclaimable in /proc/meminfo)buff/cacheSum of buffers and cacheavailableEstimation of how much memory is available for starting new applications, without swapping. Unlike thedata provided by the cache or free fields, this field takes into account page cache and also that not allreclaimable memory slabs will be reclaimed due to items being in use (MemAvailable in /proc/meminfo,available on kernels 3.14, emulated on kernels 2.6.27+, otherwise the same as free)...
可见:free命令的输出信息是基于/proc/meminfo
文件的
查看系统文件
more /proc/meminfo
wuyujin1997@budy:~$ more /proc/meminfoMemTotal: 1008720 kBMemFree:123096 kBMemAvailable:575792 kBBuffers:145400 kBCached: 398732 kBSwapCached: 0 kBActive: 622084 kBInactive: 141012 kBActive(anon):219436 kBInactive(anon):2348 kBActive(file):402648 kBInactive(file): 138664 kBUnevictable: 0 kBMlocked:0 kBSwapTotal: 0 kBSwapFree: 0 kBDirty:372 kBWriteback: 0 kBAnonPages: 219020 kBMapped: 9 kBShmem: 2820 kBSlab: 93564 kBSReclaimable:77848 kBSUnreclaim: 15716 kBKernelStack: 2988 kBPageTables: 7156 kBNFS_Unstable:0 kBBounce:0 kBWritebackTmp:0 kBCommitLimit:504360 kBCommitted_AS:906428 kBVmallocTotal: 34359738367 kBVmallocUsed: 0 kBVmallocChunk:0 kBHardwareCorrupted:0 kBAnonHugePages: 0 kBShmemHugePages: 0 kBShmemPmdMapped: 0 kBCmaTotal: 0 kBCmaFree:0 kBHugePages_Total: 0HugePages_Free: 0HugePages_Rsvd: 0HugePages_Surp: 0Hugepagesize: 2048 kBDirectMap4k: 85888 kBDirectMap2M:962560 kBDirectMap1G: 0 kBwuyujin1997@budy:~$