Intro
- 常用命令
free
free -h
以更易读(human-readable)的格式输出
free -s 3
每3秒输出一次
- 示例
wuyujin1997@budy:~$ free -h
total used free shared buff/cache available
Mem: 985M 253M 124M 2.7M 607M 566M
Swap: 0B 0B 0B
wuyujin1997@budy:~$
- 行列意义
先区分行:
Mem
真正的物理内存Swap
交换空间(物理量内存过小时利用了硬盘空间进行“中转”的空间)
再区分列:
-
total
机器的整体内存大小(物理内存 + 处于硬盘区的交换空间) -
used
已使用的内存大小,计算方式:total - free - buffers - cache
-
free
未使用/空闲的内存 -
shared
共享内存(tmpfs使用的内存) -
buff/cache
内核使用的缓冲块buffer、页面缓存cache。 -
available
估算在不使用交换空间的前提下,有多少内存可用于新启动的程序运行。 -
表格分析
total used free shared buff/cache available
Mem: 985M 253M 124M 2.7M 607M 566M
Swap: 0B 0B 0B
由表格可见,我的机器:
内存大小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
NAME
free - Display amount of free and used memory in the system
SYNOPSIS
free [options]
DESCRIPTION
free displays the total amount of free and used physical and swap memory in the system, as well as the buffers
and 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)
buffers
Memory used by kernel buffers (Buffers in /proc/meminfo)
cache Memory used by the page cache and slabs (Cached and SReclaimable in /proc/meminfo)
buff/cache
Sum of buffers and cache
available
Estimation of how much memory is available for starting new applications, without swapping. Unlike the
data provided by the cache or free fields, this field takes into account page cache and also that not all
reclaimable 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/meminfo
MemTotal: 1008720 kB
MemFree: 123096 kB
MemAvailable: 575792 kB
Buffers: 145400 kB
Cached: 398732 kB
SwapCached: 0 kB
Active: 622084 kB
Inactive: 141012 kB
Active(anon): 219436 kB
Inactive(anon): 2348 kB
Active(file): 402648 kB
Inactive(file): 138664 kB
Unevictable: 0 kB
Mlocked: 0 kB
SwapTotal: 0 kB
SwapFree: 0 kB
Dirty: 372 kB
Writeback: 0 kB
AnonPages: 219020 kB
Mapped: 92012 kB
Shmem: 2820 kB
Slab: 93564 kB
SReclaimable: 77848 kB
SUnreclaim: 15716 kB
KernelStack: 2988 kB
PageTables: 7156 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 504360 kB
Committed_AS: 906428 kB
VmallocTotal: 34359738367 kB
VmallocUsed: 0 kB
VmallocChunk: 0 kB
HardwareCorrupted: 0 kB
AnonHugePages: 0 kB
ShmemHugePages: 0 kB
ShmemPmdMapped: 0 kB
CmaTotal: 0 kB
CmaFree: 0 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
DirectMap4k: 85888 kB
DirectMap2M: 962560 kB
DirectMap1G: 0 kB
wuyujin1997@budy:~$