天天看點

buffer & cache

buffer & cache

this is an old question that i've seen from time to time. my understanding of it is rather limited (having read about the differences a long time ago, but the factoid(s) involved never really stuck).

as i understand it,

buffers

are used by programs with active i/o operations, i.e. data waiting to be written to disk

cache

is the result of completed i/o operations, i.e. buffers that have been flushed or data read from disk to satisfy a request.

the "cached" total will also include some other memory allocations, such as any tmpfs filesytems. to see this in effect try:

and you will see the "cache" value drop by the 100mb that you copied to the ram-based filesystem (assuming there was enough free ram, you might find some of it ended up in swap if the machine is already over-committed in terms of memory use). the "sync; echo

3 > /proc/sys/vm/drop_caches" before each call to free should write anything pending in all write buffers (the sync) and clear all cached/buffered disk blocks from memory so free will only be reading other allocations in the "cached" value.

the ram used by virtual machines (such as those running under vmware) will also be counted in free's "cached" value, as will ram used by currently open memory-mapped files.

so it isn't as simple as "buffers counts pending file/network writes and cached counts recently read/written blocks held in ram to save future physical reads", though for most purposes this simpler description will do.

warning this explain a strong method

not recommended on production server! so you're warned, don't blame me if something goes wrong.

for understanding, the thing, you could force your system to delegate as many memory as possible to<code>cache</code> than

drop the cached file:

preamble

before of doing the test, you could open another window an hit:

for following evolution of swap in real time.

nota: you must dispose of as many disk free on current directory, you have mem+swap

the demo

nota, the host on wich i've done this is strongly used. this will be more significant on a really quiet machine.

  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.

is shared memory?

  cache 和 buffer的差別:

  cache:高速緩存,是位于cpu與主記憶體間的一種容量較小但速度很高的存儲器。由于cpu的速度遠高于主記憶體,cpu直接從記憶體中存取資料要等待一定時間周期, cache中儲存着cpu剛用過或循環使用的一部分資料,當cpu再次使用該部分資料時可從cache中直接調用,這樣就減少了cpu的等待時間,提高了系統的效率。cache又分為一級cache(l1 cache)和二級cache(l2 cache),l1

cache內建在cpu内部,l2 cache早期一般是焊在主機闆上,現在也都內建在cpu内部,常見的容量有256kb或512kb l2 cache。

  buffer:緩沖區,一個用于存儲速度不同步的裝置或優先級不同的裝置之間傳輸資料的區域。通過緩沖區,可以使程序之間的互相等待變少,進而使從速度慢的裝置讀入資料時,速度快的裝置的操作程序不發生間斷。

  free中的buffer和cache:(它們都是占用記憶體):

  buffer : 作為buffer cache的記憶體,是塊裝置的讀寫緩沖區

  cache: 作為page cache的記憶體, 檔案系統的cache

  如果 cache 的值很大,說明cache住的檔案數很多。如果頻繁通路到的檔案都能被cache住,那麼磁盤的讀io bi會非常小。

一句話區分:readcache,writebuffer.

page cache和buffer cache最大的差别在于:page

cache是對檔案資料的緩存;buffer cache是對裝置資料的緩存!

<a target="_blank" href="http://os.51cto.com/art/200709/56603.htm">參考2</a>

上一篇: linux kernel map

繼續閱讀