buffer 與cache 的理解

2021-09-08 02:23:04 字數 2271 閱讀 2992

buffer 與cache :

通過 $ man free

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

可以看到,buffers是核心緩衝區用到的記憶體;cache是核心頁快取和slab用到的記憶體,cache大小等於cached +sreclaimable。

$man proc

buffers %lu

relatively temporary storage for raw disk blocks that shouldn't get tremendously large (20mb or so).

cached %lu

in-memory cache for files read from the disk (the page cache).  doesn't include swapcached.

...sreclaimable %lu (since linux 2.6.19)

part of slab, that might be reclaimed, such as caches.

sunreclaim %lu (since linux 2.6.19)

part of slab, that cannot be reclaimed on memory pressure.

上面這段可以簡單理解為:

cpu(寫) ------> buffer ------> disk(解決cpu與硬碟效能差異,待寫資料放入buffer池)

cpu(讀)<------ cache <------ disk(命中快取,不用每次去disk獲取資料,命中率越高,io讀(bi)越小)

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 lateruse.

$ free -m

total       used       free     sharedbufferscached

mem:          2006       1735        270          139728

-/+ buffers/cache:        967       1038

swap:            0          0          0 

# total = used + free + shared

'-buffers/cache used' = 第一行used - buffers - cached - shared;

'+buffers/cache free' = 第一行free + buffers + cached + shared;

#記憶體不足,會觸發快取**,swap機制和oom。swap 越高,說明核心置換cache越頻繁,記憶體資源越緊張。

$  vmstat 1 3 #檢視bi,bi:磁碟讀,bo:磁碟寫

procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----

r  b   swpd   free   buff  cache   si   sobibo   in   cs us sy id wa st

0  0      0 277136  40616 745644    0    088237 1188  526  7  2 91  0  0

0  0      0 277136  40632 745636    0    0     0    64 1264  628  0  0 100  0  0

0  0      0 277136  40632 745644    0    0     0     4 1258  607  0  0 100  0  0

#cache越大,bi越小

buffer與cache的理解

也就是說buffer是用於存放要輸出到disk 塊裝置 的資料的,而cache是存放從disk上讀出的資料。這二者是為了提高io效能的,並由os管理。釋放掉被系統cache占用的資料 echo 3 proc sys vm drop caches 可以通過讀取乙個大檔案來測試cache。free命令的...

buffer 與cache 的區別

有句原文解釋的很好。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.cach...

buffer 與cache 的區別

buffer 與cache 的區別 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 ...