free命令用於查看linux的記憶體使用量情況,我們先看看free的註解:
$ free -hfree: invalid option -- 'h'usage: free [-b|-k|-m|-g] [-l] [-o] [-t] [-s delay] [-c count] [-V] -b,-k,-m,-g show output in bytes, KB, MB, or GB -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 -V display version information and exit
其中,-b,-k,-m,-g代表輸出的資料使用的單位,分別是B,KB,MB,GB。
-l顯示詳細最高和最低記憶體統計。
-o不顯示-/+buffers/cache這行。
-t顯示總記憶體,total = mem + swap。
-s delay 每隔delay秒更新輸出一次。
-c count與-s配合使用,控制更新count 次。
-V大寫,列印版本號碼資訊。
$free -Vprocps version 3.2.8
我們以free -lm命令來分析free輸出的每個資料的含義,如下:
$free -lm total used free shared buffers cachedMem: 1877 1802 75 0 79 1179Low: 1877 1802 75High: 0 0 0-/+ buffers/cache: 542 1334Swap: 2015 22 1993
可以看出,使用-l命令列印出了Low和High這兩行(目前不知道為啥high都是0),我們暫且不分析這兩行,剩下的是常規輸出內容。
橫軸有:total,used,free,shared,buffers,cached。
縱軸有:Mem,Low,High,-/+buffers/cache,Swap。
total:表示物理總記憶體。
used:表示總計分配給緩衝(包含buffers與cache)使用的數量,但其中可能部分緩衝並未實際使用。
free:未被分配的記憶體。
shared:共用記憶體,是進程間互相共用的記憶體部分,一般系統不會用到,這裡不作討論。
buffers:系統分配但未被使用的buffers的數量。
cached:系統分配但未被使用的cache的數量。
Mem:實體記憶體統計。
-/+ buggers/cache:表示實體記憶體的緩衝統計。
Swap:表示硬碟上交換分區的統計(這次不作分析)。
其中:mTotal = mUsed + mFree
mTotal = bcUsed + bcFree
mUsed = bcUsed + mCached + mBuffers
bcFree = mFree + mCached + mBuffers
m和bc的區別在於,m是對於OS來說的,對於系統來說,只要被分配了,包括buffers和cached都是屬於被使用的,used中包含OS+application使用的記憶體+buffers+cached。bc是對於應用程式來說的,對於application來說,buffers+cached是等於可用的,因為buffers/cached是為了提高檔案讀取的效能,當應用程式需在用到記憶體的時候,buffer/cached會很快地被回收。