吐血推薦文章: Linux內存中的Cache真的能被回收么?
free中的buffer和cache:
兩者都是RAM中的數據。簡單來說,buffer是即將要被寫入磁盤的,而cache是被從磁盤中讀出來的。 (free中的buffer和cach它們都是占用內存的)
- A buffer is something that has yet to be "written" to disk.
- A cache is something that has been "read" from the disk and stored for later use.
buffer
buffer : 作為buffer cache的內存,是塊設備的寫緩沖區。buffer是根據磁盤的讀寫設計的,把分散的寫操作集中進行,減少磁盤碎片和硬盤的反復尋道,從而提高系統性能。linux有一個守護進程定期清空緩沖內容(即寫如磁盤),也可以通過sync命令手動清空緩沖。buffer是由各種進程分配的,被用在如輸入隊列等方面,一個簡單的例子如某個進程要求有多個字段讀入,在所有字段被讀入完整之前,進程把先前讀入的字段放在buffer中保存。
cache
cache: 作為page cache的內存, 文件系統的cache。cache經常被用在磁盤的I/O請求上,如果有多個進程都要訪問某個文件,於是該文件便被做成cache以方便下次被訪問,這樣可提供系統性能。cache是把讀取過的數據保存起來,重新讀取時若命中(找到需要的數據)就不要去讀硬盤了,若沒有命中就讀硬盤。其中的數據會根據讀取頻率進行組織,把最頻繁讀取的內容放在最容易找到的位置,把不再讀的內容不斷往后排,直至從中刪除。 如果 cache 的值很大,說明cache住的文件數很多。如果頻繁訪問到的文件都能被cache住,那么磁盤的讀IO bi會非常小。
el6:
- free 命令,在el6 el7上的輸出是不一樣的;
- 對於el6 ,看真正的有多少內存是free的,應該看 free的第二行!!!
[root@Linux ~]# free
total used free shared buffers cached
Mem: 8054344 1834624 6219720 0 60528 369948
-/+ buffers/cache: 1404148 6650196
Swap: 524280 144 524136
第1行:
total 內存總數: 8054344
used 已經使用的內存數: 1834624
free 空閑的內存數: 6219720
shared 當前已經廢棄不用,總是0
buffers Buffer Cache內存數: 60528 (緩存文件描述信息)
cached Page Cache內存數: 369948 (緩存文件內容信息)
關系:total = used + free
第2行:
-/+ buffers/cache的意思相當於:
-buffers/cache 的內存數:1404148 (等於第1行的 used - buffers - cached)
+buffers/cache 的內存數:6650196 (等於第1行的 free + buffers + cached)
可見-buffers/cache反映的是被程序實實在在吃掉的內存,而+buffers/cache反映的是可以使用的內存總數。
釋放掉被系統cache占用的數據:
-
手動執行sync命令(描述:sync 命令運行 sync 子例程。如果必須停止系統,則運行sync 命令以確保文件系統的完整性。sync 命令將所有未寫的系統緩沖區寫到磁盤中,包含已修改的 i-node、已延遲的塊 I/O 和讀寫映射文件)
有關/proc/sys/vm/drop_caches的用法在下面進行了說明:
/proc/sys/vm/drop_caches (since Linux 2.6.16)
Writing to this file causes the kernel to drop clean caches,dentries and inodes from memory, causing that memory to become free.
To free pagecache, use echo 1 > /proc/sys/vm/drop_caches;
to free dentries and inodes, use echo 2 > /proc/sys/vm/drop_caches;
to free pagecache, dentries and inodes, use echo 3 > /proc/sys/vm/drop_caches.
Because this is a non-destructive operation and dirty objects are not freeable, the user should run sync first.
echo 3>/proc/sys/vm/drop_caches
el7
[root@jiangyi01.sqa.zmf /home/ahao.mah]
#free -lm
total used free shared buff/cache available
Mem: 96479 6329 84368 201 5781 89491
Low: 96479 12110 84368
High: 0 0 0
Swap: 2047 2047 0
[root@jiangyi01.sqa.zmf /home/ahao.mah]
#free -k;cat /proc/meminfo | grep MemAvailable
total used free shared buff/cache available
Mem: 98795000 6481796 86390012 206496 5923192 91638016
Swap: 2097148 2096352 796
MemAvailable: 91638016 kB
shared : Memory used (mostly) by tmpfs (Shmem in /proc/meminfo, available on kernels 2.6.32, displayed as zero if not available)
total = used + free + buff/cache
available = free + buff/cache(部分)
el7中free的available其實對應:cat /proc/meminfo | grep MemAvailable
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)
理解buffer和cache
我們可以使用dd命令去測試
首先生成一個1G的大文件
[root@jiangyi02.sqa.zmf /home/ahao.mah]
#dd if=/dev/zero of=bigfile bs=1M count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 1.71586 s, 611 MB/s
[root@jiangyi02.sqa.zmf /home/ahao.mah]
#du -sh bigfile
1001M bigfile
清空緩存
[root@jiangyi02.sqa.zmf /home/ahao.mah]
#echo 3 | tee /proc/sys/vm/drop_caches
3
[root@jiangyi02.sqa.zmf /home/ahao.mah]
#free -m
total used free shared buffers cached
Mem: 96839 1695 95144 0 6 46
-/+ buffers/cache: 1642 95196
Swap: 2047 0 2047
讀入這個文件,測試消耗的時間
[root@jiangyi02.sqa.zmf /home/ahao.mah]
#time cat bigfile > /dev/null
real 0m6.770s
user 0m0.005s
sys 0m0.477s
[root@jiangyi02.sqa.zmf /home/ahao.mah]
#free -m
total used free shared buffers cached
Mem: 96839 2709 94130 0 10 1051
-/+ buffers/cache: 1647 95192
Swap: 2047 0 2047
再次讀入該文件,測試消耗的時間
[root@jiangyi02.sqa.zmf /home/ahao.mah]
#time cat bigfile > /dev/null
real 0m0.235s
user 0m0.005s
sys 0m0.230s
對比,有了cache緩存后,第二次讀的速度提高了28倍,這就是cache的力量
[root@jiangyi02.sqa.zmf /home/ahao.mah]
#echo "scale=3;6770/235" |bc
28.808