Linux監控工具介紹系列——free


    在Linux系統中,我們查看、監控系統內存使用情況,一般最常用的命令就是free。free命令其實非常簡單,參數也非常簡單,但是里面很多知識點未必你都掌握了。下面總結一下我所了解的free命令。如有不足,敬請指出。文章很多知識點參考了下面參考資料,都是在這些前輩文章的基礎上所做的一個驗證和總結。

free命令介紹

free命令是一個顯示系統中空閑和已用內存大小的工具。大多數Linux發行版都包含有free命令,但是版本可能不一樣。free 命令使用/proc/meminfo中的值作為基准來顯示內存利用率信息。free命令的英文介紹:free - displays the total amount of free and used physical and swap memory in the system, as well as the buffers used by the kernel. The shared memory column represents the ’Shmem’ value. The available memory column represents the ’MemAvailable’ value.

free參數介紹

你可以在控制台下輸入man free命令查看更多關於free命令的信息。另外你可以使用下面命令獲取free的使用信息(雖然是一個無效參數)

[root@localhost ~]# free --help
free: invalid option -- '-'
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
  -a show available memory if exported by kernel (>80 characters per line)
  -V display version information and exit

free命令的參數有下面一些,不同版本可能有所區別(procps version 3.2.8):

參數

功能

-b -k -m -g

分別以ByteKBMBGB為單位顯示內存的使用情況

-l

show detailed low and high memory statistics

-o

使用舊的格式顯示內存的使用信息,沒有描述 -/+buffers/cache信息這一行。

-t

顯示內存總和列

-s

<間隔秒數>  持續觀察內存使用狀況。

-c

結合參數-s使用,表示更新多少次。update [count] times

-a

show available memory if exported by kernel (>80 characters per line)

-V

顯示free命令版本信息並退出該命令

例子:

1:以MB為單位顯示內存的使用情況

[root@localhost ~]# free -m
             total       used       free     shared    buffers     cached
Mem:         11912        957      10955          1        167        498
-/+ buffers/cache:        291      11620
Swap:        12287          0      12287
[root@localhost ~]# 

2:每隔3秒顯示內存的使用信息

[root@localhost ~]# free -m -s 3
             total       used       free     shared    buffers     cached
Mem:         11912        957      10955          1        167        498
-/+ buffers/cache:        292      11620
Swap:        12287          0      12287
 
             total       used       free     shared    buffers     cached
Mem:         11912        957      10955          1        167        498
-/+ buffers/cache:        292      11620
Swap:        12287          0      12287
 
             total       used       free     shared    buffers     cached
Mem:         11912        957      10955          1        167        498
-/+ buffers/cache:        292      11620
Swap:        12287          0      12287
 
             total       used       free     shared    buffers     cached
Mem:         11912        957      10955          1        167        498
-/+ buffers/cache:        292      11620
Swap:        12287          0      12287

clip_image001

3:顯示內存總和列

 
[root@localhost ~]# free -mt
             total       used       free     shared    buffers     cached
Mem:         11912        957      10955          1        167        498
-/+ buffers/cache:        292      11620
Swap:        12287          0      12287
Total:       24200        957      23243
[root@localhost ~]# 

clip_image002

4:顯示高低內存利用率

[root@localhost ~]# free -ml
             total       used       free     shared    buffers     cached
Mem:         11912        957      10955          1        167        498
Low:         11912        957      10955
High:            0          0          0
-/+ buffers/cache:        292      11620
Swap:        12287          0      12287
[root@localhost ~]# 

5:每2秒顯示一次內存使用情況,一共顯示3次

[root@localhost ~]# free -s 2 -c 3
             total       used       free     shared    buffers     cached
Mem:      12198496     981976   11216520       1176     171260     510160
-/+ buffers/cache:     300556   11897940
Swap:     12582908          0   12582908
 
             total       used       free     shared    buffers     cached
Mem:      12198496     981976   11216520       1176     171260     510160
-/+ buffers/cache:     300556   11897940
Swap:     12582908          0   12582908
 
             total       used       free     shared    buffers     cached
Mem:      12198496     981976   11216520       1176     171260     510160
-/+ buffers/cache:     300556   11897940
Swap:     12582908          0   12582908

 

free指標介紹

free命令一般顯示4行信息,使用參數-t則會顯示5行信息(-l 參數會顯示6行)。你是否真正理解每一項指標的意義呢?這個我覺得可能不少人不甚了解。那么下面就來看看free命令輸出信息的具體含義吧:

 

1:第一行從全局角度描述系統使用的內存狀況:

指標

指標意義

total

總的物理內存大小。total=used + free

used

已經使用的物理內存。一般情況這個值比較大。used=buffers + cached (maybe add shared also)+  -buffers/cache(used)

free

完全未使用的物理內存

shared

應用程序共享的物理內存

buffers

緩存,主要用於塊設備緩存,例如用戶目錄、inode值等(ls大目錄可以看到這個值增加)

cached

緩存,主要用於緩存文件。

 

 

相信很多人和我一樣,對buffers 和cached有點混淆不清。下面是對buffers和cached的一個較權威的解釋。

    buffers是指用來給塊設備做的緩沖大小,它只記錄文件系統的metadata以及 tracking in-flight pages.

    cached是用來給文件做緩沖。

    那就是說:buffers是用來存儲目錄里面有什么內容,權限等等。而cached直接用來緩存我們打開的文件

 

下面是關於buffers和cached的英文資料:

Buffers are associated with a specific block device, and cover caching of filesystem metadata as well as tracking in-flight pages. The cache only contains parked file data. That is, the buffers remember what's in directories, what file permissions are, and keep track of what memory is being written from or read to for a particular block device. The cache only contains the contents of the files themselves.

也就是說buffers是用於存放要輸出到disk的塊設備數據的,而cache是存放從disk上讀出的數據。這二者是為了提高IO性能的,並由OS管理。

下面我們可以使用ls /dev命令來驗證一下buffers增加的實驗測試。如下所示。buffers從171140增加到了171144

clip_image003

關於cached,當應用程序讀寫文件的時候,Linux內核為了提高讀寫效率與速度,會將文件在內存中進行緩存,這部分內存就是Cache Memory(緩存內存)。即使你的程序運行結束后,Cache Memory也不會自動釋放。這就會導致你在Linux系統中程序頻繁讀寫文件后,你會發現可用物理內存變少。前陣子我就碰到這樣一個案例,有位網友說他服務器上沒有跑什么應用程序,但是內存都被耗盡了。其實真正的原因只是內存被用來做buffer和cached緩存數據了,而他不了解情況,以為free列就是剩余的內存,以為內存被耗盡了。如果free部分很小,但是buffer和cached部分較大的話,完全沒有必要擔心,緩存內存(Cache Memory)在你需要使用內存的時候會自動釋放。

Linux always tries to use RAM to speed up disk operations by using available memory for buffers (file system metadata) and cache (pages with actual contents of files or block devices). This helps the system to run faster because disk information is already in memory which saves I/O operations. If space is needed by programs or applications like Oracle, then Linux will free up the buffers and cache to yield memory for the applications. If your system runs for a while you will usually see a small number under the field "free" on the first line.

 

關於buffers與cached,Don't Panic! Your ram is fine! 這篇文章對這個有個非常有意思的描述。我簡單翻譯一下,大家對比英文看看吧

What's going on?

Linux is borrowing unused memory for disk caching. This makes it looks like you are low on memory, but you are not! Everything is fine!

Linux系統從空閑的內存借用內存來做磁盤數據緩存。這個導致你的系統看起來處於內存非常緊急的情況。但是實際上不是這樣。一切都非常正常。

Why is it doing this?

Disk caching makes the system much faster! There are no downsides, except for confusing newbies. It does not take memory away from applications in any way, ever!

磁盤數據緩存讓Linux運行得更快。它不會也永遠不會從應用程序程序拿走內存。它沒有任何缺點,只是會混淆新手。

What if I want to run more applications?

If your applications want more memory, they just take back a chunk that the disk cache borrowed. Disk cache can always be given back to applications immediately! You are not low on ram!

如果你的應用程序需要更多的內存,他們會收回一部分用作磁盤數據緩存(disk cache)的物理內存。磁盤數據緩存能很快的將內存返回給應用程序使用。

Do I need more swap?

No, disk caching only borrows the ram that applications don't currently want. It will not use swap. If applications want more memory, they just take it back from the disk cache. They will not start swapping.

不, 磁盤數據緩存只會從應用程序哪里借用不需要使用的物理內存。它不會使用交換分區。如果應用程序需要跟多內存。他們會從磁盤數據緩存里面拿回需要的物理內存。它們將不啟動交換。

How do I stop Linux from doing this?

You can't disable disk caching. The only reason anyone ever wants to disable disk caching is because they think it takes memory away from their applications, which it doesn't! Disk cache makes applications load faster and run smoother, but it NEVER EVER takes memory away from them! Therefore, there's absolutely no reason to disable it!

 

 

Disk caching是不能禁用的。但是可以使用下面命令釋放memory cached:

To free pagecache:
 
echo 1 > /proc/sys/vm/drop_caches
 
To free dentries and inodes:
 
echo 2 > /proc/sys/vm/drop_caches
 
To free pagecache, dentries and inodes:
 
echo 3 > /proc/sys/vm/drop_caches

 

2: 第二行則是第一行各個指標的值:

 

3: 第三行描述應用程序的內存使用情況:

指標

指標意義

前個值表示-buffers/cache

應用程序使用的內存大小,used減去緩存值:-buffers/cache=used-buffers-cached

后個值表示+buffers/cache

所有可供應用程序使用的內存大小,free加上緩存值:+buffers/cache=free+buffers+cached

 

4: 第四行描述swap使用情況:

指標

指標意義

total

全部交換分區(swap

used

已使用交換分區(swap

free

未使用交換分區(swap

 

如下圖所示,我們來驗證一下各指標的關系,看看各指標是否如上述公式所將的那樣:

clip_image004

total = used + free

12198496 = 982224 + 11216272

used = buffers + cached (maybe add shared also) + -buffers/cache(used):

982224 =171412 + 510180 + 300632

-buffers/cache(used) 表示第三行的used列的值。-buffers/cache(free) 表示第三行free列的值。

-buffers/cache(used) = used - buffers - cached

-buffers/cache(free) = free+buffers+cached

11897864 = 11216272 + 171412 + 510180

 

參考資料:

http://www.ha97.com/4337.html

http://www.linuxatemyram.com/

https://linux.cn/article-2443-1.html

http://www.cnblogs.com/ggjucheng/archive/2012/01/08/2316438.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM