歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> 每天一個linux命令(45):free命令

每天一個linux命令(45):free命令

日期:2017/3/1 17:09:04   编辑:關於Linux
每天一個linux命令(45):free命令 相關鏈接: 每天一個linux命令(1):ls命令 http://www.2cto.com/os/201210/163049.html; 每天一個linux命令(2):cd命令 http://www.2cto.com/os/201210/163050.html; 每天一個linux命令(3):pwd命令 http://www.2cto.com/os/201210/163462.html; 每天一個linux命令(4):mkdir命令 http://www.2cto.com/os/201210/163463.html; 每天一個linux命令(5):rm命令 http://www.2cto.com/os/201210/163662.html; 每天一個linux命令(6):rmdir命令 http://www.2cto.com/os/201210/164017.html; 每天一個linux命令(7):mv命令 http://www.2cto.com/os/201210/164247.html; 每天一個linux命令(8):cp命令 http://www.2cto.com/os/201210/164254.html; 每天一個linux命令(9):touch命令 http://www.2cto.com/os/201211/165699.html; 每天一個linux命令(10):cat命令 http://www.2cto.com/os/201211/165989.html; 每天一個linux命令(11):nl命令 http://www.2cto.com/os/201211/165990.html 每天一個linux命令(12):more命令 http://www.2cto.com/os/201211/165994.html 每天一個linux命令(13):less命令 http://www.2cto.com/os/201211/165998.html 每天一個linux命令(14):head命令 http://www.2cto.com/os/201211/166191.html 每天一個linux命令(15):tail命令 http://www.2cto.com/os/201211/168702.html 每天一個linux命令(16):which命令 http://www.2cto.com/os/201211/168890.html 每天一個linux命令(17):whereis命令 http://www.2cto.com/os/201211/168893.html 每天一個linux命令(18):locate命令 http://www.2cto.com/os/201211/168895.html 每天一個linux命令(19):find命令概覽 http://www.2cto.com/os/201211/168897.html 每天一個linux命令(20):find命令之exec http://www.2cto.com/os/201211/168901.html 每天一個linux命令(21):find命令之xargs http://www.2cto.com/os/201211/168903.html 每天一個linux命令(22):find命令的參數詳解 http://www.2cto.com/os/201211/168912.html 每天一個linux命令(23):Linux目錄結構 http://www.2cto.com/os/201211/170430.html 每天一個linux命令(24):Linux文件類型與擴展名 http://www.2cto.com/os/201211/170431.html 每天一個linux命令(25):linux文件屬性詳解 http://www.2cto.com/os/201211/170434.html 每天一個linux命令(26):用SecureCRT來上傳和下載文件 http://www.2cto.com/os/201211/172022.html 每天一個linux命令(27):linux chmod命令 http://www.2cto.com/os/201211/172028.html 每天一個linux命令(28):tar命令 http://www.2cto.com/os/201212/172641.html 每天一個linux命令(29):chgrp命令 http://www.2cto.com/os/201212/172983.html 每天一個linux命令(30):chown命令 http://www.2cto.com/os/201212/173239.html 每天一個linux命令(31):/etc/group文件詳解 http://www.2cto.com/os/201212/174429.html 每天一個linux命令(32):gzip命令 http://www.2cto.com/os/201212/174431.html 每天一個linux命令(33):df命令 http://www.2cto.com/os/201212/174434.html 每天一個linux命令(34):du命令 http://www.2cto.com/os/201212/174701.html 每天一個linux命令(35):ln命令 http://www.2cto.com/os/201212/174993.html 每天一個linux命令(36):diff命令 http://www.2cto.com/os/201212/176333.html 每天一個linux命令(37):date命令 http://www.2cto.com/os/201212/176335.html 每天一個linux命令(38):cal命令 http://www.2cto.com/os/201212/176337.html 每天一個linux命令(39):grep命令 http://www.2cto.com/os/201212/177059.html 每天一個linux命令(40):wc命令 http://www.2cto.com/os/201212/177343.html 每天一個linux命令(41):ps命令 http://www.2cto.com/os/201212/177665.html 每天一個linux命令(42):kill命令 http://www.2cto.com/os/201212/177686.html 每天一個linux命令(43):killall命令 http://www.2cto.com/os/201212/178264.html 每天一個linux命令(44):top命令 http://www.2cto.com/os/201212/179026.html free命令可以顯示Linux系統中空閒的、已用的物理內存及swap內存,及被內核使用的buffer。在Linux系統監控的工具中,free命令是最經常使用的命令之一。 www.2cto.com 1.命令格式: free [參數] 2.命令功能: free 命令顯示系統使用和空閒的內存情況,包括物理內存、交互區內存(swap)和內核緩沖區內存。共享內存將被忽略 3.命令參數: -b  以Byte為單位顯示內存使用情況。 -k  以KB為單位顯示內存使用情況。 -m  以MB為單位顯示內存使用情況。 -g 以GB為單位顯示內存使用情況。 -o  不顯示緩沖區調節列。 -s<間隔秒數>  持續觀察內存使用狀況。 -t  顯示內存總和列。 -V  顯示版本信息。 4.使用實例: 實例1:顯示內存使用情況 命令: free free -g free -m 輸出: [root@SF1150 service]# free total used free shared buffers cached Mem: 32940112 30841684 2098428 0 4545340 11363424 -/+ buffers/cache: 14932920 18007192 Swap: 32764556 1944984 30819572 [root@SF1150 service]# free -g total used free shared buffers cached Mem: 31 29 2 0 4 10 -/+ buffers/cache: 14 17 Swap: 31 1 29 [root@SF1150 service]# free -m total used free shared buffers cached Mem: 32168 30119 2048 0 4438 11097 -/+ buffers/cache: 14583 17584 Swap: 31996 1899 30097 說明: 下面是對這些數值的解釋: total:總計物理內存的大小。 used:已使用多大。 free:可用有多少。 Shared:多個進程共享的內存總額。 Buffers/cached:磁盤緩存的大小。 第三行(-/+ buffers/cached): used:已使用多大。 free:可用有多少。 第四行是交換分區SWAP的,也就是我們通常所說的虛擬內存。 區別:第二行(mem)的used/free與第三行(-/+ buffers/cache) used/free的區別。 這兩個的區別在於使用的角度來看,第一行是從OS的角度來看,因為對於OS,buffers/cached 都是屬於被使用,所以他的可用內存是2098428KB,已用內存是30841684KB,其中包括,內核(OS)使用+Application(X, oracle,etc)使用的+buffers+cached. 第三行所指的是從應用程序角度來看,對於應用程序來說,buffers/cached 是等於可用的,因為buffer/cached是為了提高文件讀取的性能,當應用程序需在用到內存的時候,buffer/cached會很快地被回收。 所以從應用程序的角度來說,可用內存=系統free memory+buffers+cached。 如本機情況的可用內存為: 18007156=2098428KB+4545340KB+11363424KB 接下來解釋什麼時候內存會被交換,以及按什麼方交換。 當可用內存少於額定值的時候,就會開會進行交換.如何看額定值: 命令: cat /proc/meminfo 輸出: [root@SF1150 service]# cat /proc/meminfo MemTotal: 32940112 kB MemFree: 2096700 kB Buffers: 4545340 kB Cached: 11364056 kB SwapCached: 1896080 kB Active: 22739776 kB Inactive: 7427836 kB HighTotal: 0 kB HighFree: 0 kB LowTotal: 32940112 kB LowFree: 2096700 kB SwapTotal: 32764556 kB SwapFree: 30819572 kB Dirty: 164 kB Writeback: 0 kB AnonPages: 14153592 kB Mapped: 20748 kB Slab: 590232 kB PageTables: 34200 kB NFS_Unstable: 0 kB Bounce: 0 kB CommitLimit: 49234612 kB Committed_AS: 23247544 kB VmallocTotal: 34359738367 kB VmallocUsed: 278840 kB VmallocChunk: 34359459371 kB HugePages_Total: 0HugePages_Free: 0HugePages_Rsvd: 0Hugepagesize: 2048 kB 交換將通過三個途徑來減少系統中使用的物理頁面的個數:  1.減少緩沖與頁面cache的大小, 2.將系統V類型的內存頁面交換出去,  3.換出或者丟棄頁面。(Application 占用的內存頁,也就是物理內存不足)。 事實上,少量地使用swap是不是影響到系統性能的。 那buffers和cached都是緩存,兩者有什麼區別呢? 為了提高磁盤存取效率, Linux做了一些精心的設計, 除了對dentry進行緩存(用於VFS,加速文件路徑名到inode的轉換), 還采取了兩種主要Cache方式:Buffer Cache和Page Cache。前者針對磁盤塊的讀寫,後者針對文件inode的讀寫。這些Cache有效縮短了 I/O系統調用(比如read,write,getdents)的時間。 www.2cto.com 磁盤的操作有邏輯級(文件系統)和物理級(磁盤塊),這兩種Cache就是分別緩存邏輯和物理級數據的。 Page cache實際上是針對文件系統的,是文件的緩存,在文件層面上的數據會緩存到page cache。文件的邏輯層需要映射到實際的物理磁盤,這種映射關系由文件系統來完成。當page cache的數據需要刷新時,page cache中的數據交給buffer cache,因為Buffer Cache就是緩存磁盤塊的。但是這種處理在2.6版本的內核之後就變的很簡單了,沒有真正意義上的cache操作。 Buffer cache是針對磁盤塊的緩存,也就是在沒有文件系統的情況下,直接對磁盤進行操作的數據會緩存到buffer cache中,例如,文件系統的元數據都會緩存到buffer cache中。 簡單說來,page cache用來緩存文件數據,buffer cache用來緩存磁盤數據。在有文件系統的情況下,對文件操作,那麼數據會緩存到page cache,如果直接采用dd等工具對磁盤進行讀寫,那麼數據會緩存到buffer cache。 所以我們看linux,只要不用swap的交換空間,就不用擔心自己的內存太少.如果常常swap用很多,可能你就要考慮加物理內存了.這也是linux看內存是否夠用的標准. 如果是應用服務器的話,一般只看第二行,+buffers/cache,即對應用程序來說free的內存太少了,也是該考慮優化程序或加內存了。 實例2:以總和的形式顯示內存的使用信息 命令: free -t 輸出: [root@SF1150 service]# free -t total used free shared buffers cached Mem: 32940112 30845024 2095088 0 4545340 11364324 -/+ buffers/cache: 14935360 18004752Swap: 32764556 1944984 30819572Total: 65704668 32790008 32914660[root@SF1150 service]# 說明: 實例3:周期性的查詢內存使用信息 命令: www.2cto.com free -s 10 輸出: [root@SF1150 service]# free -s 10 total used free shared buffers cached Mem: 32940112 30844528 2095584 0 4545340 11364380 -/+ buffers/cache: 14934808 18005304Swap: 32764556 1944984 30819572 total used free shared buffers cached Mem: 32940112 30843932 2096180 0 4545340 11364388 -/+ buffers/cache: 14934204 18005908Swap: 32764556 1944984 30819572 說明: 每10s 執行一次命令
Copyright © Linux教程網 All Rights Reserved