例子
[root@VM_16_17_centos bin]# free total used free shared buff/cache available Mem: 1882892 785272 280428 40496 817192 852060 Swap: 0 0 0
先說明一些基本概念
第一列Mem
內存的使用信息Swap
交換空間的使用信息
第一行total
系統總的可用物理內存大小used
已被使用的物理內存大小free
還有多少物理內存可用shared
被共享使用的物理內存大小buff/cache
被 buffer 和 cache 使用的物理內存大小available
還可以被 應用程序 使用的物理內存大小
其中有兩個概念需要注意
free 與 available 的區別
free
是真正尚未被使用的物理內存數量。available
是應用程序認為可用內存數量,available = free + buffer + cache
(注:只是大概的計算方法)
Linux 為了提升讀寫性能,會消耗一部分內存資源緩存磁盤數據,對於內核來說,buffer 和 cache 其實都屬於已經被使用的內存。但當應用程序申請內存時,如果 free 內存不夠,內核就會回收 buffer 和 cache 的內存來滿足應用程序的請求。這就是稍后要說明的 buffer 和 cache。
buff 和 cache 的區別
以下內容來自# buffer和cache怎么讓你們解釋的那么難理解?
從字面上和語義來看,buffer名為緩沖,cache名為緩存。我們知道各種硬件存在制作工藝上的差別,所以當兩種硬件需要交互的時候,肯定會存在速度上的差異,而且只有交互雙方都完成才可以各自處理別的其他事務。假如現在有兩個需要交互的設備A和B,A設備用來交互的接口速率為1000M/s,B設備用來交互的接口速率為500M/s,那他們彼此訪問的時候都會出現以下兩種情況:(以A來說)
一.A從B取一個1000M的文件結果需要2s,本來需要1s就可以完成的工作,卻還需要額外等待1s,B設備把剩余的500M找出來,這等待B取出剩下500M的空閑時間內(1s)其他的事務還干不了
二.A給B一個1000M的文件結果也需要2s,本來需要也就1s就可以完成的工作,卻由於B,1s內只能拿500M,剩下的500M還得等下一個1sB來取,這等待下1s的時間還做不了其他事務。
那有什么方法既可以讓A在‘取’或‘給’B的時候既能完成目標任務又不浪費那1s空閑等待時間去處理其他事務呢?我們知道產生這種結果主要是因為B跟不上A的節奏,但即使這樣A也得必須等B處理完本次事務才能干其他活(單核cpu來說),除非你有三頭六臂。那有小伙伴可能會問了,能不能在A和B之間加一層區域比如說ab,讓ab既能跟上A的頻率也會照顧B的感受,沒錯我們確實可以這樣設計來磨合接口速率上的差異,你可以這樣想象,在區域ab提供了兩個交互接口一個是a接口另一個是b接口,a接口的速率接近A,b接口的速率最少等於B,然后我們把ab的a和A相連,ab的b和B相連,ab就像一座橋把A和B鏈接起來,並告知A和B通過他都能轉發給對方,文件可以暫時存儲,最終拓撲大概如下:

現在我們再來看上述兩種情況:
對於第一種情況A要B:當A從B取一個1000M的文件,他把需求告訴了ab,接下來ab通過b和B進行文件傳送,由於B本身的速率,傳送第一次ab並沒有什么卵用,對A來說不僅浪費了時間還浪費了感情,ab這家伙很快感受到了A的不滿,所以在第二次傳送的時候,ab背着B偷偷緩存了一個一模一樣的文件,而且只要從B取東西,ab都會緩存一個拷貝下來放在自己的大本營,如果下次A或者其他C來取B的東西,ab直接就給A或C一個貨真價實的贗品,然后把它通過a接口給了A或C,由於a的速率相對接近A的接口速率,所以A覺得不錯為他省了時間,最終和ab的a成了好基友,說白了此時的ab提供的就是一種緩存能力,即cache,絕對的走私!因為C取的是A執行的結果。所以在這種工作模式下,怎么取得的東西是最新的也是我們需要考慮的,一般就是清cache。例如cpu讀取內存數據,硬盤一般都提供一個內存作為緩存來增加系統的讀取性能
對於第二種情況A給B:當A發給B一個1000M的文件,因為A知道通過ab的a接口就可以轉交給B,而且通過a接口要比通過B接口傳送文件需要等待的時間更短,所以1000M通過a接口給了ab ,站在A視圖上他認為已經把1000M的文件給了B,但對於ab並不立即交給B,而是先緩存下來,除非B執行sync命令,即使B馬上要,但由於b的接口速率最少大於B接口速率,所以也不會存在漏洞時間,但最終的結果是A節約了時間就可以干其他的事務,說白了就是推卸責任,哈哈而ab此時提供的就是一種緩沖的能力,即buffer,它存在的目的適用於當速度快的往速度慢的輸出東西。例如內存的數據要寫到磁盤,cpu寄存器里的數據寫到內存。
看了上面這個例子,那我們現在看一下在計算機領域,在處理磁盤IO讀寫的時候,cpu,memory,disk基於這種模型給出的一個實例。我們先來一幅圖:(我從別家當來的,我覺得,看N篇文檔 不如瞄此一圖)

page cache:文件系統層級的緩存,從磁盤里讀取的內容是存儲到這里,這樣程序讀取磁盤內容就會非常快,比如使用grep和find等命令查找內容和文件時,第一次會慢很多,再次執行就快好多倍,幾乎是瞬間。但如上所說,如果對文件的更新不關心,就沒必要清cache,否則如果要實施同步,必須要把內存空間中的cache clean下
buffer cache:磁盤等塊設備的緩沖,內存的這一部分是要寫入到磁盤里的。這種情況需要注意,位於內存buffer中的數據不是即時寫入磁盤,而是系統空閑或者buffer達到一定大小統一寫到磁盤中,所以斷電易失,為了防止數據丟失所以我們最好正常關機或者多執行幾次sync命令,讓位於buffer上的數據立刻寫到磁盤里。
man free
FREE(1) User Commands FREE(1)
NAME
free - Display amount of free and used memory in the system
SYNOPSIS
free [options]
DESCRIPTION
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)
buffers
Memory used by kernel buffers (Buffers in /proc/meminfo)
cache Memory used by the page cache and slabs (Cached and SReclaimable 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
provided by the cache or free fields, this field takes into account page cache and also that not all
reclaimable memory slabs will be reclaimed due to items being in use (MemAvailable in /proc/meminfo, avail‐
able on kernels 3.14, emulated on kernels 2.6.27+, otherwise the same as free)
OPTIONS
-b, --bytes
Display the amount of memory in bytes.
-k, --kibi
Display the amount of memory in kibibytes. This is the default.
-m, --mebi
Display the amount of memory in mebibytes.
-g, --gibi
Display the amount of memory in gibibytes.
--tebi Display the amount of memory in tebibytes.
--pebi Display the amount of memory in pebibytes.
--kilo Display the amount of memory in kilobytes. Implies --si.
--mega Display the amount of memory in megabytes. Implies --si.
--giga Display the amount of memory in gigabytes. Implies --si.
--tera Display the amount of memory in terabytes. Implies --si.
--peta Display the amount of memory in petabytes. Implies --si.
-h, --human
Show all output fields automatically scaled to shortest three digit unit and display the units of print out.
Following units are used.
B = bytes
K = kibibyte
M = mebibyte
G = gibibyte
T = tebibyte
P = pebibyte
If unit is missing, and you have exbibyte of RAM or swap, the number is in tebibytes and columns might not be
aligned with header.
-w, --wide
Switch to the wide mode. The wide mode produces lines longer than 80 characters. In this mode buffers and
cache are reported in two separate columns.
-c, --count count
Display the result count times. Requires the -s option.
-l, --lohi
Show detailed low and high memory statistics.
-s, --seconds delay
Continuously display the result delay seconds apart. You may actually specify any floating point number for
delay using either . or , for decimal point. usleep(3) is used for microsecond resolution delay times.
--si Use kilo, mega, giga etc (power of 1000) instead of kibi, mebi, gibi (power of 1024).
-t, --total
Display a line showing the column totals.
--help Print help.
-V, --version
Display version information.
FILES
/proc/meminfo
memory information
BUGS
The value for the shared column is not available from kernels before 2.6.32 and is displayed as zero.
Please send bug reports to
⟨procps@freelists.org⟩
SEE ALSO
ps(1), slabtop(1), top(1), vmstat(8).
procps-ng 2016-06-03 FREE(1)
--- free(1) - Linux manual page
FREE(1) User Commands FREE(1)
NAME top
free - Display amount of free and used memory in the system
SYNOPSIS top
free [options]
DESCRIPTION top
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) buffers Memory used by kernel buffers (Buffers in /proc/meminfo) cache Memory used by the page cache and slabs (Cached and SReclaimable 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 provided by the cache or free fields, this field takes into account page cache and also that not all reclaimable memory 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)
OPTIONS top
-b, --bytes Display the amount of memory in bytes. -k, --kibi Display the amount of memory in kibibytes. This is the default. -m, --mebi Display the amount of memory in mebibytes. -g, --gibi Display the amount of memory in gibibytes. --tebi Display the amount of memory in tebibytes. --pebi Display the amount of memory in pebibytes. --kilo Display the amount of memory in kilobytes. Implies --si. --mega Display the amount of memory in megabytes. Implies --si. --giga Display the amount of memory in gigabytes. Implies --si. --tera Display the amount of memory in terabytes. Implies --si. --peta Display the amount of memory in petabytes. Implies --si. -h, --human Show all output fields automatically scaled to shortest three digit unit and display the units of print out. Following units are used. B = bytes Ki = kibibyte Mi = mebibyte Gi = gibibyte Ti = tebibyte Pi = pebibyte If unit is missing, and you have exbibyte of RAM or swap, the number is in tebibytes and columns might not be aligned with header. -w, --wide Switch to the wide mode. The wide mode produces lines longer than 80 characters. In this mode buffers and cache are reported in two separate columns. -c, --count count Display the result count times. Requires the -s option. -l, --lohi Show detailed low and high memory statistics. -s, --seconds delay Continuously display the result delay seconds apart. You may actually specify any floating point number for delay using either . or , for decimal point. usleep(3) is used for microsecond resolution delay times. --si Use kilo, mega, giga etc (power of 1000) instead of kibi, mebi, gibi (power of 1024). -t, --total Display a line showing the column totals. --help Print help. -V, --version Display version information.
FILES top
/proc/meminfo memory information
BUGS top
The value for the shared column is not available from kernels before 2.6.32 and is displayed as zero. Please send bug reports to ⟨procps@freelists.org⟩
SEE ALSO top
ps(1), slabtop(1), top(1), vmstat(8).
COLOPHON top
This page is part of the procps-ng (/proc filesystem utilities) project. Information about the project can be found at ⟨https://gitlab.com/procps-ng/procps⟩. If you have a bug report for this manual page, see ⟨https://gitlab.com/procps-ng/procps/blob/master/Documentation/bugs.md⟩. This page was obtained from the project's upstream Git repository ⟨https://gitlab.com/procps-ng/procps.git⟩ on 2021-08-27. (At that time, the date of the most recent commit that was found in the repository was 2021-08-24.) If you discover any rendering problems in this HTML version of the page, or you believe there is a better or more up-to-date source for the page, or you have corrections or improvements to the information in this COLOPHON (which is not part of the original manual page), send a mail to man-pages@man7.org procps-ng 2018-05-31 FREE(1)