inux page buffer cache深入理解
 

<span style="line-height: 1.5; background-color: inherit;">Page cache和buffer cache一直以来是两个比较容易混淆的概念,在网上也有很多人在争辩和猜想这两个cache到底有什么区别,讨论到最后也一直没有一个统一和正确的结论,在我工作的这一段时间,page cache和buffer cache的概念曾经困扰过我,但是仔细分析一下,这两个概念实际上非常的清晰。如果能够了解到这两个cache的本质,那么我们在分析io问题的时候可能会更加得心应手。</span>

&nbsp;

<span style="font: 14px/21px 微软雅黑; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;">Page cache实际上是针对文件系统的,是文件的缓存,<span style="background-color: inherit;"><span style="background-color: inherit;">在文件层面上的数据</span>(在文件系统构架里,组成一个文件的有两部分:文件的元数据,即FCB,还有文件的业务数据,即比如平时我们用记事本程序打开一个文件所看到的数据。&ldquo;<span style="background-color: inherit;"><span style="background-color: inherit;">在文件层面上的数据</span></span>&rdquo;指的就是<span style="background-color: inherit;">文件的业务数据</span>)</span>会缓存到page cache(即<span style="background-color: inherit;"><span style="background-color: inherit;">page cache规划就是用来缓存<span style="background-color: inherit;"><span style="background-color: inherit;">来自在文件系统下的任一文件的业务数据所存放的那些扇区)</span></span></span></span>)。文件的逻辑层需要映射到实际的物理磁盘,这种映射关系由文件系统来完成。<span style="background-color: inherit;">当page cache的数据需要刷新时,page cache中的数据交给buffer cache,</span>但是这种处理在2.6版本的内核之后就变的很简单了,没有真正意义上的cache操作(是说直接将<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">文件的业务数据</span></span></span><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">(以扇区为<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">读写的基本单位</span></span></span>方式,这个方式也是<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">数据</span></span></span></span>从磁盘读取到buffer cache上的方式)</span></span></span></span>读取到<span style="background-color: inherit;"><span style="background-color: inherit;">page cache</span></span>上,不经过<span style="background-color: inherit;"><span style="background-color: inherit;">buffer cache</span></span>?)。</span>

&nbsp;

&nbsp;

<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">Buffer cache是针对<span style="background-color: inherit;"><span style="background-color: inherit;">磁盘块(即扇区)</span></span>的缓存(<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">即buffer cache允许缓存<span style="background-color: inherit;"><span style="background-color: inherit;">的数据可以是来自一个磁盘上任意一个扇区的数据</span></span></span></span></span></span>),也就是在没有文件系统的情况下(准确的表达是,无论有否<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">文件系统</span></span></span>的存在,<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">设备驱动程序</span></span></span>读取的数据(也即<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">直接对磁盘进行操作的数据</span></span></span>)都是<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">缓存到buffer cache中</span>的</span></span>),直接对磁盘进行操作的数据会缓存到buffer cache中,<span style="background-color: inherit;">例如,<span style="background-color: inherit;">文件系统的元数据</span>都会缓存到buffer cache中。</span></span></span></span>

<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">也就是说,首先我们要知道<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">进程</span></span></span></span></span>(0)(即设备驱动程序,或设备文件)对磁盘进行读写的基本单位是扇区(不是其他大小,如文件系统块大小),所以内存上(的是无论<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">page cache</span></span>,还是<span style="background-color: inherit;"><span style="background-color: inherit;">buffer cache</span></span></span>)<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">对磁盘进行读写的基本单位是扇区。</span>进程(1)对<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">Buffer cache</span></span></span></span></span><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">进行读写的基本单位也是扇区,<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">进程(2)对<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">page cache</span></span></span></span></span></span></span></span>进行读写的基本单位则是<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">文件系统块。</span></span></span></span></span></span></span>例如,<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">buffer cache</span></span></span></span></span></span><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">通过进程</span></span></span></span></span>(0)(即设备驱动程序)</span></span></span></span></span></span></span></span>从磁盘上</span></span></span></span></span></span></span></span><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">读</span></span></span></span></span>取一些数据(这些数据可能分散开的,也可能连续一起,如果更巧的话,正好连续成一个文件系统块的大小)</span></span></span>是<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">以<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">扇区</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>为基本单位</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>,接着<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">page cache</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">通过进程</span></span></span></span></span>(1)(非设备驱动程序了,是一般的程序了)</span></span></span></span></span></span></span></span>从<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">buffer cache</span></span></span></span></span></span></span></span></span></span></span>上</span></span></span></span></span></span></span></span><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">将这些数据读</span></span></span></span></span>取到自己上也</span></span></span>是<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">以<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">扇区</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>为基本单位的,之后,</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">进程(2)<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">(非设备驱动程序了,是一般的程序了)</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>从<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">page cache</span></span></span></span></span></span></span></span>上</span></span></span></span></span></span></span></span></span></span></span></span></span></span><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">将这些数据读</span></span></span></span></span>取到自己上时则</span></span></span>是<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">以<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">文件系统块</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>为基本单位的</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span style="background-color: inherit;">&nbsp;</span>。&nbsp;<span style="background-color: inherit;">&nbsp;</span></span></span></span>

<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">Buffer cache</span></span></span>算是属于硬盘分区这个层次的一部分,<span style="background-color: inherit;">&nbsp;</span><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">page cache</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>&nbsp;<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">算是属于硬盘分区这个层次的上一层即文件系统这个层次的一部分</span></span></span><span style="background-color: inherit;">&nbsp;</span>。文件系统对磁盘有一个自己的格式划分(即以文件系统块为划分单位),但是<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">文件系统</span></span></span>(的进程)不能直接去读写磁盘(即就不可能以<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">文件系统块为单位来直接读写</span></span></span>磁盘上的数据),<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">文件系统</span></span></span>(的进程)</span></span></span>只能调用驱动程序去读写<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">磁盘上的数据</span></span></span>,<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">驱动程序直接读写<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">磁盘上的数据</span></span></span></span></span></span>是以<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">扇区</span></span></span></span></span></span></span></span></span>为基本单位的,而</span></span></span></span></span></span>直接对磁盘进行操作的数据则是(程序编程时规划)缓存到buffer cache中。</span></span></span>

<span style="background-color: inherit;">简单说来,page cache用来缓存<span style="background-color: inherit;"><span style="background-color: inherit;">文件数据(即文件的业务数据)</span></span>,buffer cache用来缓存<span style="background-color: inherit;"><span style="background-color: inherit;">磁盘数据</span></span>(例如,<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">在<span style="background-color: inherit;"><span style="background-color: inherit;">文件系统</span></span>这个层次上,</span>文件系统的元数据</span></span>(比如,FAT文件系统中的文件分配表FAT就是属于文件系统的元数据),文件的元数据(即FCB,文件控制块(块,广义的概念是指一片存储区域,无关这片存储区域多大,即无关这片存储区域是否为如文件系统块或扇区的整数倍,小于<span style="background-color: inherit;">它们</span>或不是它们的整数倍都可以,像文件控制块就是128字节大小));在硬盘分区这个层次上,<span style="background-color: inherit;"><span style="background-color: inherit;">主引导记录(Master Boot Record,缩写:MBR),又叫做主引导扇区,</span>分区表(DPT,Disk Partition Table)</span></span>)。<span style="background-color: inherit;">在有文件系统的情况下,对文件操作,那么数据会缓存到page cache,如果直接采用dd等工具对磁盘进行读写,那么数据会缓存到buffer cache。&nbsp;</span>

&nbsp;

<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">更简单说来,</span></span></span>

<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">buffer cache允许缓存<span style="background-color: inherit;"><span style="background-color: inherit;">的数据可以是来自一个磁盘上任意一个扇区的数据,而</span></span>page cache则规划就是用来缓存<span style="background-color: inherit;"><span style="background-color: inherit;">来自在文件系统下的任一文件的业务数据所存放的那些扇区。</span></span></span></span></span>

&nbsp;

<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">补充一点,</span></span>在文件系统层每个设备都会分配一个def_blk_ops的文件操作方法,这是设备的操作方法,在每个设备的inode下面会存在一个radix tree,这个radix tree下面将会放置缓存数据的page页。这个page的数量将会在top程序的buffer一栏中显示。如果设备做了文件系统,那么会生成一个inode,这个inode会分配ext3_ops之类的操作方法,这些方法是文件系统的方法,在这个inode下面同样存在一个radix tree,这里会缓存文件的page页,缓存页的数量在top程序的cache一栏进行统计。<span style="background-color: inherit;">从上面的分析可以看出,2.6内核中的buffer cache和page cache<span style="background-color: inherit;">在处理上是保持一致</span>(即对两个cache上的数据的操作方法类似)的,但是存在概念上的差别(即操作或叫针对的对象不一样),page cache针对文件的cache,buffer是针对磁盘块数据的cache,仅此而已。</span></span>

&nbsp;

&nbsp;

<span style="font: 14px/21px 微软雅黑; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;">buffer 与cache 的区别<span style="background-color: inherit;">&nbsp;</span>

A buffer is something that has yet to be &ldquo;written&rdquo; to disk. A cache is something that has been &ldquo;read&rdquo; from the disk and stored for later use.

更详细的解释参考:Difference Between Buffer and Cache<span style="background-color: inherit;">&nbsp;</span>

对于共享内存(Shared memory),主要用于在UNIX 环境下不同进程之间共享数据,是进程间通信的一种方法,一般的应用程序不会申请使用共享内存,笔者也没有去验证共享内存对上面等式的影响。如果你有兴趣,请参考:What is Shared Memory?</span>

&nbsp;

<span style="font: 14px/21px 微软雅黑; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;">cache 和 buffer的区别:<span style="background-color: inherit;">&nbsp;</span>

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.<span style="background-color: inherit;">&nbsp;</span>

Buffer:缓冲区,一个用于存储速度不同步的设备或优先级不同的设备之间传输数据的区域。通过缓冲区,可以使进程之间的相互等待变少,从而使从速度慢的设备读入数据时,速度快的设备的操作进程不发生间断。

<span style="background-color: inherit;"><span style="background-color: inherit;">Free中的buffer和cache:(它们都是占用内存):<span style="background-color: inherit;">&nbsp;</span>

buffer :作为buffer cache的内存,是块设备的读写缓冲区<span style="background-color: inherit;">&nbsp;</span>

cache(名词):作为page cache的内存, 文件系统的cache<span style="background-color: inherit;">&nbsp;</span></span></span></span>

<span style="background-color: inherit;"><span style="background-color: inherit;">如果 cache<span style="background-color: inherit;"><span style="background-color: inherit;"><span style="background-color: inherit;">(名词)</span></span></span><span style="background-color: inherit;">&nbsp;</span>的值很大,说明cache住的文件数很多。如果频繁访问到的文件都能被cache(动词)住,那么磁盘的读IO 必会非常小。</span></span>