日期:2014-05-16  浏览次数:20804 次

linux:磁盘缓冲区和页高速缓存的联系与区别
摘自:http://stackoverflow.com/questions/6345020/linux-memory-buffer-vs-cache



概念上的区别:
  1. buffer是块设备的内存读写缓冲区,而page cache是文件系统的概念。
  2. 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.
 
但事实上: 
  1. 由于linux使用了页回写技术,所以现在page cache也起到了buffer类似的作用:先把数据写到page cache中并置为"dirty",后台进程在适当的时间再把它冲刷到磁盘上。
  2. 既然两者的角色很模糊,所以在linux2.4以后,两者在内存里是同一块区域。在2.4之间,数据会被缓存两次,浪费内存;2.4以后,数据只会缓存一次了。不过,buffer的概念仍然保留着,shell中使用free命令仍然能看到buffer项的存在。
 
 
引用

Short answer: Cached is the size of the page cache. Buffers is the size of in-memory block I/O buffers. Cached matters; Buffers is largely irrelevant.

Long answer: Cached is the size of the Linux page cache, minus the memory in the swap cache, which is represented by SwapCached (thus the total page cache size is Cached + SwapCached). Linux performs all file I/O through the page cache. Writes are implemented as simply marking as dirty the corresponding pages in the page cache; the flusher threads then periodically write back to disk any dirty pages. Reads are implemented by returning the data from the page cache; if the data is not yet in the cache, it is first populated. On a modern Linux system, Cached can easily be several gigabytes. It will shrink only in response to memory pressure. The system will purge the page cache along with swapping data out to disk to make available more memory as needed.

Buffers are in-memory block I/O buffers. They are relatively short-lived. Prior to Linux kernel version 2.4, Linux had separate page and buffer caches. Since 2.4, the page and buffer cache are unified ...