free命令可选参数
- -b,-k,-m,-g show output in bytes, KB, MB, or GB
- -h human readable output (automatic unit scaling)
- -l show detailed low and high memory statistics
- -o use old format (no -/+buffers/cache line)
- -t display total for RAM + swap
- -s update every [delay] seconds
- -c update [count] times
- -a show available memory if exported by kernel (>80 characters per line)
- -V display version information and exit
常用参数演示
[root@ ~]# free -b #以Byte为单位显示
total used free shared buffers cached
Mem: 1044611072 552603648 492007424 57344 39006208 124108800
-/+ buffers/cache: 389488640 655122432
Swap: 1572855808 245936128 1326919680
[root@ ~]# free -k #以KB为单位显示
total used free shared buffers cached
Mem: 1020128 536520 483608 56 38108 121200
-/+ buffers/cache: 377212 642916
Swap: 1535992 240172 1295820
[root@ ~]# free -m #以MB为单位显示
total used free shared buffers cached
Mem: 996 523 472 0 37 118
-/+ buffers/cache: 368 627
Swap: 1499 234 1265
[root@ ~]# free -h #人性化输出显示
total used free shared buffers cached
Mem: 996M 521M 474M 56K 35M 118M
-/+ buffers/cache: 368M 628M
Swap: 1.5G 234M 1.2G
[root@ ~]# free -o #不显示-/+buffers/cache line,不建议
total used free shared buffers cached
Mem: 1020128 536892 483236 56 38468 121284
Swap: 1535992 240172 1295820
[root@ ~]# free -th #算上swap
total used free shared buffers cached
Mem: 996M 524M 471M 56K 37M 118M
-/+ buffers/cache: 368M 627M
Swap: 1.5G 234M 1.2G
Total: 2.4G 758M 1.7G
[root@ ~]# time free -s 1 -c 3 #表示持续每隔1s更新更新一次mem信息,更新3次
total used free shared buffers cached
Mem: 1020128 537512 482616 56 38868 121328
-/+ buffers/cache: 377316 642812
Swap: 1535992 240172 1295820
total used free shared buffers cached
Mem: 1020128 537512 482616 56 38868 121328
-/+ buffers/cache: 377316 642812
Swap: 1535992 240172 1295820
total used free shared buffers cached
Mem: 1020128 537512 482616 56 38868 121328
-/+ buffers/cache: 377316 642812
Swap: 1535992 240172 1295820
real 0m2.002s
user 0m0.000s
sys 0m0.001s
[root@ ~]#
free 命令输出结果信息说明
total used free shared buffers cached
Mem: 996M 525M 470M 56K 38M 118M
-/+ buffers/cache: 368M 627M
Swap: 1.5G 234M 1.2G
第一行
- total: 内存总数
- used: 已经使用内存数
- free: 完全空闲内存
- shared: 多个进程共享的内存
- buffers: 用于块设备数据缓冲,记录文件系统metadata(目录,权限,属性等)
- cached: 用于文件内容的缓冲
第一列
- Mem: 物理内存
- -/+ buffers/cache: 基于应用角度考虑(计算已使用内存时减去buffers/cache,计算可使用内存时加上buffers/cache)的内存情况,也可理解为真实的内存使用情况.
- Swap: 交换分区
当我们获取系统内存用量的时候我们应该以“-/+ buffers/cached”行的used和free作为参考.因为第一行的buffers和cached被系统作为了缓存(这里包括缓冲了metadata数据和曾经打开过的内容,是为了加快我们系统处理的速度),而这部分缓存可以根据我们的应用内存使用情况随时释放掉(也可以手动释放).
这里的话我系统可用内存实际为:可用627M,已使用368M,而不是525M和470M.
buffers/cahed手动释放测试
释放前
[root@ ~]# free -h total used free shared buffers cached Mem: 996M 531M 465M 56K 42M 119M -/+ buffers/cache: 369M 626M Swap: 1.5G 234M 1.2G [root@ ~]#
使用命令手动释放
[root@ ~]# echo 3 > /proc/sys/vm/drop_caches [root@ ~]# free -h total used free shared buffers cached Mem: 996M 391M 604M 56K 420K 26M -/+ buffers/cache: 364M 631M Swap: 1.5G 234M 1.2G [root@ ~]#
手动释放后我们看到 第一行Mem的used和free有了很大的提升,我们上述的观点得到了验证
使用find 命令让buffers增加
[root@ backup]# free -h;find .>/dev/null;free -h total used free shared buffers cached Mem: 996M 397M 598M 56K 2.6M 32M -/+ buffers/cache: 362M 633M Swap: 1.5G 234M 1.2G
total used free shared buffers cached Mem: 996M 415M 581M 56K 17M 32M -/+ buffers/cache: 364M 631M Swap: 1.5G 234M 1.2G [root@ backup]#
使用cat 命令让cached增加
[root@zwj python]# free -h;find /mydata/backup/python/ -type f|xargs cat>/dev/null 2>&1;free -h total used free shared buffers cached Mem: 996M 434M 561M 56K 18M 50M -/+ buffers/cache: 365M 630M Swap: 1.5G 234M 1.2G total used free shared buffers cached Mem: 996M 767M 228M 56K 20M 367M -/+ buffers/cache: 379M 617M Swap: 1.5G 234M 1.2G
Done!!!