Linux命令---free

free

換一個套路,我們先看man free中對free的描述:


Display amount of free and used memory in the system

翻譯:顯示系統中使用的和未用的內存數量

我們再來看一下命令執行結果

              total        used        free      shared  buff/cache   available
Mem:       16131568     8461796     1418932      848140     6250840     6352668
Swap:       8191996          24     8191972

man free中對各個表頭的解釋如下

    free displays the total amount of free and used physical and swap
       memory in the system, as well as the buffers and caches  used  by
       the kernel. The information is gathered by parsing /proc/meminfo.
       The displayed columns are:

       total  Total  installed  memory  (MemTotal   and   SwapTotal   in
              /proc/meminfo)

       used   Used memory (calculated as total - free - buffers - cache)

       free   Unused memory (MemFree and SwapFree in /proc/meminfo)

       shared Memory  used  (mostly)  by  tmpfs (Shmem in /proc/meminfo,
              available on kernels 2.6.32,  displayed  as  zero  if  not
              available)

       buffers
              Memory used by kernel buffers (Buffers in /proc/meminfo)

       cache  Memory  used  by the page cache and slabs (Cached and Slab
              in /proc/meminfo)

       buff/cache
              Sum of buffers and cache

       available
              Estimation of how much memory is  available  for  starting
              new  applications,  without swapping. Unlike the data pro‐
              vided by the cache or free fields, this field  takes  into
              account  page cache and also that not all reclaimable mem‐
              ory slabs will be reclaimed due  to  items  being  in  use
              (MemAvailable in /proc/meminfo, available on kernels 3.14,
              emulated on kernels 2.6.27+, otherwise the same as free)

我們來分別解釋一下(其實就是翻譯一下),

free命令顯示系統中所有使用的和未使用的物理內存和交換分區的數量(這裏大概提一下,交換區作用是的當內存不夠的時候,部分不常使用的信息被暫時存儲到交換區,待使用的時候再放到內存中),以及內核使用的緩衝區和緩存(buffer和caches分不清的話,可以理解爲buffer是一些放在內存中還沒有刷到磁盤的緩存數據,而caches是從磁盤中讀出到內存的數據。這裏不展開介紹了),這些信息來源於解析 /proc/meminfo ,顯示的屬性如下:

 - total        系統安裝的總內存
 - used         使用的內存 計算方法爲:total - free - buffers - cache
 - free         未使用的內存 
 - shared       (最可能是)被tmpfs文件系統使用的內存 一般不會用到
 - buffers      buffers使用的內存數
 - cache        cache和slab使用的內存數(slab是linux下的一種內存分配機制,可以避免頻繁的分配同類內存,即可以循環利用)
 - buff/cache   buffers和cache的和
 - available    估計有多少內存在啓動新的應用時可以被用到

接下來我們學習一下free可攜帶的參數

Options:
 -b, --bytes         show output in bytes           使用字節顯示
 -k, --kilo          show output in kilobytes       使用kb顯示
 -m, --mega          show output in megabytes       使用mb顯示
 -g, --giga          show output in gigabytes       使用gb顯示
     --tera          show output in terabytes       使用tb
 -h, --human         show human-readable output     使用人類易讀的方式
     --si            use powers of 1000 not 1024    使用一千字節計數
 -l, --lohi          show detailed low and high memory statistics   顯示最高最低的統計信息
 -t, --total         show total for RAM + swap          顯示物理內存和交換區總數
 -s N, --seconds N   repeat printing every N seconds    每幾分鐘重複顯示一次
 -c N, --count N     repeat printing N times, then exit 重複輸出n次然後退出(每秒一次)
 -w, --wide          wide output    buffers和cache會分開顯示

     --help     display this help and exit          幫助
 -V, --version  output version information and exit 版本信息

常用命令運行結果如下:

free -h
              total        used        free      shared  buff/cache   available
Mem:            15G        8.1G        1.2G        836M        6.1G        6.0G
Swap:          7.8G         24K        7.8G
dev1:~$ free -s 5
              total        used        free      shared  buff/cache   available
Mem:       16131568     8507812     1217156      856332     6406600     6299660
Swap:       8191996          24     8191972

^C
dev1:~$ free -hs 5
              total        used        free      shared  buff/cache   available
Mem:            15G        8.1G        1.2G        836M        6.1G        6.0G
Swap:          7.8G         24K        7.8G

^C
dev1:~$ free -w
              total        used        free      shared     buffers       cache   available
Mem:       16131568     8484328     1240332      856332           0     6406908     6323160
Swap:       8191996          24     8191972

dev1:~$ free -wh
              total        used        free      shared     buffers       cache   available
Mem:            15G        8.1G        1.2G        836M          0B        6.1G        6.0G
Swap:          7.8G         24K        7.8G

free -s 5 -c 2 //五秒運行一次,一共運行兩次就退出
              total        used        free      shared  buff/cache   available
Mem:       16131568     8535816     1179516      856364     6416236     6271488
Swap:       8191996          24     8191972

              total        used        free      shared  buff/cache   available
Mem:       16131568     8539412     1175796      856364     6416360     6267892
Swap:       8191996          24     8191972

其他的運行結果就不給了,給了的話就有點刷文章長度的嫌疑。大概說一下常用的結合用法吧

  1. -h 命令是比較常用的 可以與其他的-w -s -c配合使用
  2. -s 和-c是可以同時使用的 例如 free -s 5 -c 2 //五秒運行一次,一共運行兩次
  3. --si 是需要和-b -k -m -g這些一起使用,不再以1024爲進位標準而是1000
  4. 其他的請大家自由組合嘗試吧
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章