free命令使用(內存計算)


最近在看服務器性能的參數,free命令總是用得很暈。

之所以暈,是因為不同版本的free打印的數據項目不一樣,數據也不一樣。

之前看了v3.2.8的,查查資料,弄明白了。后來看v3.3.10,又不一樣了,這次就專門整理記錄下。

 

一、先熟悉下free 的命令參數  

free --help

出來結果:

free: invalid option -- '-'
usage: free [-b|-k|-m|-g|-h] [-l] [-o] [-t] [-s delay] [-c count] [-V]
-b,-k,-m,-g show output in bytes, KB, MB, or GB       
-h human readable output (automatic unit scaling)
-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

1)-b,-k,-m,-g show output in bytes, KB, MB, or GB

按照bytes, KB, MB, or GB作為單位來輸出,

例如:

free -m
             total       used       free     shared    buffers     cached
Mem:         16080      15858        222          1        322      13278
-/+ buffers/cache:       2258      13822
Swap:         8191        381       7810
free -g
             total       used       free     shared    buffers     cached
Mem:            15         15          0          0          0         12
-/+ buffers/cache:          2         13
Swap:            7          0          7

2)-h human readable output (automatic unit scaling)

人性化輸出,例如:

free -h
             total       used       free     shared    buffers     cached
Mem:           15G        15G       228M       1.9M       322M        12G
-/+ buffers/cache:       2.2G        13G
Swap:         8.0G       381M       7.6G

3)-l show detailed low and high memory statistics

不知道啥意思..

4) use old format (no -/+buffers/cache line)

不顯示-/+buffers/cache 這一行。v3.3.10沒有這個參數選項。

5)-t display total for RAM + swap

顯示內存+交換空間的總內存,t是total的意思吧。例:

free -ht
             total       used       free     shared    buffers     cached
Mem:           15G        15G       225M       1.9M       322M        12G
-/+ buffers/cache:       2.2G        13G
Swap:         8.0G       381M       7.6G
Total:         23G        15G       7.8G

感覺也沒多大意義

6)-s update every [delay] seconds

不斷輸出,指定間隔秒數。不過這里不一定是整數,0.5也可以! 

比如 free -h -s 10 ,就是10秒輸出一次。

7)-c update [count] times

不斷輸出,指定輸出次數。

比如  free -h -c 10,就是輸出10次。

但是在版本 v3.2.8,就是輸出一次!需要配合 -s 食用。

在版本 v3.3.10,不加-s,就默認1秒輸出一次。其他版本怎么樣,不清楚。

比如free -h -s 2 -c 10,每隔2秒輸出一回,輸出10次。

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

不知道啥意思..,不過

在版本3.3.10,有一個參數是

-w, --wide          wide output。

就是把buffers 和cache 分開顯示,因為在3.3.10中,默認打印的是(buffers + cache)的

9)-V display version information and exit

 查看版本號。

 

二、數據查看

舉例:

v3.2.8

free -h
             total       used       free     shared    buffers     cached
Mem:           15G        15G       263M       1.9M       322M        12G
-/+ buffers/cache:       2.2G        13G
Swap:         8.0G       381M       7.6G
v3.3.10
 free -h
              total        used        free      shared  buff/cache   available
Mem:            15G        5.9G        626M        1.2G        9.1G        8.0G
Swap:           29G         28M         29G
free -wh
              total        used        free      shared     buffers       cache   available
Mem:            15G        5.9G        610M        1.2G        2.8G        6.3G        8.0G
Swap:           29G         28M         29G

 

1.total:總可用物理內存。一般是總物理內存除去一些預留的和操作系統本身的內存占用,是操作系統可以支配的內存大小。這個在v3.2.8和v3.3.10一樣。這個值是/proc/meminfo中MemTotal的值。
2.used:已使用的物理內存。
在v3.2.8,這個值是(total - free)得出來的。可以說是系統已經被系統分配,但是實際並不一定正在被真正的使用,其空間可以被回收再分配的。
在v3.3.10,這個值是(total - free - cache - buffers)得出來的,是真正目前正在被使用的內存。
3.free:系統還未使用的物理內存。這個值是/proc/meminfo中MemFree的值。
4.shared:共享內存的空間。這個值是/proc/meminfo中Shmem的值。
5.buffers:用來給塊設備做緩存的大小。這個值是/proc/meminfo中Buffers的值。
6.cached:v3.2.8中的項。這個值是/proc/meminfo中Cached的值。
cache:v3.3.10中的項。這個值是/proc/meminfo中(Cached + Slab)的值。
     在buff/cache是(buffers+cache)的值。free -w可以分開顯示。
7.available:v3.3.10中的項。看起來這個值是可以使用的內存,不過(available + used) < total,也就是available < (free + cache + buffers)。
       而在v3.2.8中(free + cache + buffers)是一般認為的可用內存,既然在新版本中有這個available數據,應該是更准確的吧。畢竟並不是所有的未使用的內存就一定是可用的。
       這個值是取的/proc/meminfo中MemAvailable的值,如果meminfo中沒有這個值,會依據meminfo中的Active(file),Inactive(file),MemFree,SReclaimable等值計算一個。

-/+ buffers/cache
v3.2.8有這一行,v3.3.10 沒有。
其中,used 這一項是(used - buffers - cached)的值,即(total - free - buffers - cached)的值,是真正在使用的內存的值。
free 這一項是(free + buffers + cached)的值,是真正未使用的內存的值。
個人覺得有 -/+ buffers/cache,這一欄看的挺習慣。。不過新版本v3.3.10的used更明確。相信有不少人和我一樣,剛看到v3.2.8里面的used占了這么多內存的時候,有點摸不着頭腦。

總結一下:
(1) 使用 free v3.2.8,看內存占用,兩個辦法:
1.看 -/+ buffers/cache 這一行。used 是已使用的內存;free 是未使用的內存。
2.看 Mem 這一行。(used - buffers - cached) 是已使用的內存;(free + buffers + cached) 是未使用的內存。
(2) 使用 free v3.3.10,看內存占用:
看 Mem 這一行。used 是已使用的內存;(free + buff/cache),即(total - used)是未使用的內存;available 是剩余可以使用的內存。未使用的內存不代表都可用,所以 available <(total - used).
 
        


2019-10-29 18:32:53







免責聲明!

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



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