在Linux/Unix 系統上,free是一個很受系統管理員歡迎的命令,它是一個功能強大的工具,他能以一種易讀的方式把內存使用情況展示出來
free 展示了系統中空閒和已經使用了的物理內存、交換內存以及內核的緩衝區和頁面緩存,所有展示的信息都是從 /proc/meminfo 文件解析出來的
語法free命令的語法如下
free [options]
輸出列說明當只輸入free 命令不帶任何選項時,輸出的內存和交換內存大小是以 KB(kbytes)為單位的,具體的輸出如下所示(在舊版的Linux系統中,顯示可能略有不同):
[root@ecs-centos-7 ~]# free
total used free shared buff/cache available
Mem: 1881544 325980 1307868 8712 247696 1406892
Swap: 0 0 0總內存的大小,可以用於應用程式的內存
已經使用內存,計算方式是:used = total - free - buff/cache
還沒有使用的內存
進程之間的共享內存
內核緩衝區和頁面緩存,如果應用程式需要的話,可以隨時回收這部分緩存,通過 free -w 命令可以分別顯示 buff 和 cache 佔用的內存
可用內存的預估大小,可以用於啟動新的應用程式,實際應用中,可以把 free 和 buff/cache 加起來看做 available 的近似值,即 free + buff/cache ≈ available
常用的選項以下是一些常用的選項
[root@ecs-centos-7 ~]# free -h
total used free shared buffers cache available
Mem: 1881544 353752 881684 8712 147960 498148 1370492
Swap: 0 0 0[root@ecs-centos-7 ~]# free -t
total used free shared buff/cache available
Mem: 1881544 354108 881328 8712 646108 1370136
Swap: 0 0 0
Total: 1881544 354108 881328[root@ecs-centos-7 ~]# free -s 2
total used free shared buff/cache available
Mem: 1881544 353960 881476 8712 646108 1370284
Swap: 0 0 0
total used free shared buff/cache available
Mem: 1881544 353984 881452 8712 646108 1370260
Swap: 0 0 0
total used free shared buff/cache available
Mem: 1881544 353984 881452 8712 646108 1370260
Swap: 0 0 0例子中的命令是每隔2秒輸出一次內存信息,直到按 Ctrl + Z 停止
[root@ecs-centos-7 ~]# free -c 3
total used free shared buff/cache available
Mem: 1881544 353960 881476 8712 646108 1370284
Swap: 0 0 0
total used free shared buff/cache available
Mem: 1881544 353984 881452 8712 646108 1370260
Swap: 0 0 0
total used free shared buff/cache available
Mem: 1881544 353984 881452 8712 646108 1370260
Swap: 0 0 0上面的例子是重複輸出內存信息3次,每次間隔默認是1秒,如果要修改默認輸出間隔可以加上 -s 秒數,下面的命令是:重複輸出3次,每次輸出間隔2秒
free -c 3 -s 2[root@ecs-centos-7 ~]# free -w
total used free shared buffers cache available
Mem: 1881544 354100 881328 8712 147968 498148 1370144
Swap: 0 0 0[root@ecs-centos-7 ~]# free -b
total used free shared buff/cache available
Mem: 1926701056 362446848 902631424 8921088 661622784 1403179008
Swap: 0 0 0
[root@ecs-centos-7 ~]# free -k
total used free shared buff/cache available
Mem: 1881544 354100 881328 8712 646116 1370144
Swap: 0 0 0
[root@ecs-centos-7 ~]# free -m
total used free shared buff/cache available
Mem: 1837 345 860 8 630 1338
Swap: 0 0 0
[root@ecs-centos-7 ~]# free -g
total used free shared buff/cache available
Mem: 1 0 0 0 0 1
Swap: 0 0 0上面的例子中,分別以 Bytes、KB、MB、GB 為單位輸出內存信息,會自動忽略小於對應單位的數值,比如 free -g命令,只有 total 和 available 列的值大於 1GB ( 1024 * 1024 * 1024 Bytes ), 其他列的值都是小於 1GB,所以 total 和 available 列顯示 1,其他列都顯示 0
實際還有多少可用內存[root@ecs-centos-7 ~]# free -h
total used free shared buff/cache available
Mem: 1.8G 534M 100M 8.5M 1.3G 1.5G
Swap: 0B 0B 0B在上面的示例中,如果只是看 used 以及 free 的話,會以為系統可用內存已經不足100M,也即可使用內存不足 1%了
實際上,已經被應用程式使用的只有 27%左右(534M / 1.8G), 應用程式可用內存是 availabe 或者 free + buff/cache ,也就是說例子中實際可用於應用程式的內存有 1.5G 之多
內存什麼時候告急在平常的伺服器監控內存的過程中,有以下幾個信號是需要引起注意的
availabe 或者 free + buff/cache 表示實際應用程式的可用內存,如果它接近於0的話,表示應用程式可用內存不足,需要儘快處理
已使用交換內存一直增長的話,有可能是物理內存不足的先兆,當物理內存長時間不足的時候,才會頻繁的使用交換內存,導致已使用交換內存一直增長
為了防止系統物理內存不夠用的時候系統崩潰,當檢測到內存不足時,系統會 kill 掉最佔用內存的進程,/var/log/message 中會記錄 Out of memory的日誌
推薦閱讀