参考:10 ‘free’ Commands to Check Memory Usage in Linux
简介
free 命令查看物理内存(physical memory)、交换内存(swap memory)和内核缓冲区内存(buffers used by kernel in Linux/Unix)使用情况。
用法
无参数
[root@localhost ~]# free total used free shared buff/cache available Mem: 8157208 326528 7175756 11856 654924 7517932 Swap: 4063228 0 4063228
默认输出,单位是KB,你也可以显式使用-k参数指定。当然-b使用Byte单位,-m使用MB单位,-g使用GB
但是这些单位,会影响所有数据,还是不够人性化。-h显示更人性化,输出你可以看到KB、MB、GB等单位
[root@localhost ~]# free -h total used free shared buff/cache available Mem: 7.8G 318M 6.8G 11M 639M 7.2G Swap: 3.9G 0B 3.9G
定时定次输出
-s参数可以设置定时输出(每隔多久输出),默认值是1s
-c参数可以设置定次输出,累计输出多少次退出
free -s 2 -c 5
每隔2s输出一次,共输出5次
total used free shared buff/cache available Mem: 8157208 326792 7175380 11856 655036 7517672 Swap: 4063228 0 4063228 total used free shared buff/cache available Mem: 8157208 326792 7175380 11856 655036 7517672 Swap: 4063228 0 4063228 total used free shared buff/cache available Mem: 8157208 326792 7175380 11856 655036 7517672 Swap: 4063228 0 4063228 total used free shared buff/cache available Mem: 8157208 326840 7175332 11856 655036 7517624 Swap: 4063228 0 4063228 total used free shared buff/cache available Mem: 8157208 326824 7175348 11856 655036 7517640 Swap: 4063228 0 4063228
额外备注
free输出,关于仔细看的会发现内存比实际物理内存容量小。其原因是free命令显示的总内存是除去内核文件占用内存和一些系统保留的内存之后能使用的内存
使用dmesg查看内存,获得的内存信息基本上就是真实物理内存大小了。