Linux上free命令的输出及其他

一、明确概念

    • 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是用于存放要输出到disk(块设备)的数据的,而cache是存放从disk上读出的数据。

 

从应用场景上看:Buffer 更多的(场景)是减小写操作的冲击,而 Cache 主要用于减小读 I/O 的重复开销。

 

The terms "buffer" and "cache" tend to be used interchangeably,  note however they represent different things.

(1)A buffer is used traditionally as an intermediate temporary store for data between a fast and a slow entity.

As one party would have to wait for the other affecting performance, the buffer alleviates this by allowing entire

blocks of data to move at once rather then in small chunks. The data is written and read only once from the buffer.

Furthermore, the buffers are visible to at least one party which is aware of it.


 (2)A cache on the other hand by definition is hidden and neither party is aware that caching occurs. It as well

improves performance but does that by allowing the same data to be read multiple times in a fast fashion.


A further explanation of the differences between two can be found here.

 

 

二、命令输出

             total       used       free     shared    buffers     cached
Mem:      498            154        343          0     23         65
-/+ buffers/cache:       65         433
Swap:      1023          0          1023

    Men:total = Men:used + Men:free

   已使用     -buffers/cache = Mem:used - Men:buffers - Men:cached  = 154-23-65 = 154-88 = 66  (1024byte = 1M )

   还剩余     +buffers/cache = Men:free  + Men:buffers + Men:cached  = 343+23+65 = 343+88 = 431 

 

buff/cache

先来提一个问题: buffer 和 cache 应该是两种类型的内存,但是 free 命令为什么会把它们放在一起呢?要回答这个问题需要我们做些准备工作。让我们先来搞清楚 buffer 与 cache 的含义。

buffer 在操作系统中指 buffer cache, 中文一般翻译为 "缓冲区"。要理解缓冲区,必须明确另外两个概念:"扇区" 和 "块"。扇区是设备的最小寻址单元,也叫 "硬扇区" 或 "设备块"。块是操作系统中文件系统的最小寻址单元,也叫 "文件块" 或 "I/O 块"。每个块包含一个或多个扇区,但大小不能超过一个页面,所以一个页可以容纳一个或多个内存中的块。当一个块被调入内存时,它要存储在一个缓冲区中。每个缓冲区与一个块对应,它相当于是磁盘块在内存中的表示(下图来自互联网):

注意,buffer cache 只有块的概念而没有文件的概念,它只是把磁盘上的块直接搬到内存中而不关心块中究竟存放的是什么格式的文件。

cache 在操作系统中指 page cache,中文一般翻译为 "页高速缓存"。页高速缓存是内核实现的磁盘缓存。它主要用来减少对磁盘的 I/O 操作。具体地讲,是通过把磁盘中的数据缓存到物理内存中,把对磁盘的访问变为对物理内存的访问。页高速缓存缓存的是内存页面。缓存中的页来自对普通文件、块设备文件(这个指的就是 buffer cache 呀)和内存映射文件的读写
页高速缓存对普通文件的缓存我们可以这样理解:当内核要读一个文件(比如 /etc/hosts)时,它会先检查这个文件的数据是不是已经在页高速缓存中了。如果在,就放弃访问磁盘,直接从内存中读取。这个行为称为缓存命中。如果数据不在缓存中,就是未命中缓存,此时内核就要调度块 I/O 操作从磁盘去读取数据。然后内核将读来的数据放入页高速缓存中。这种缓存的目标是文件系统可以识别的文件(比如 /etc/hosts)。
页高速缓存对块设备文件的缓存就是我们在前面介绍的 buffer cahce。因为独立的磁盘块通过缓冲区也被存入了页高速缓存(缓冲区最终是由页高速缓存来承载的)。

到这里我们应该搞清楚了:无论是缓冲区还是页高速缓存,它们的实现方式都是一样的。缓冲区只不过是一种概念上比较特殊的页高速缓存罢了。
那么为什么 free 命令不直接称为 cache 而非要写成 buff/cache? 这是因为缓冲区和页高速缓存的实现并非天生就是统一的。在 linux 内核 2.4 中才将它们统一。更早的内核中有两个独立的磁盘缓存:页高速缓存和缓冲区高速缓存。前者缓存页面,后者缓存缓冲区。当你知道了这些故事之后,输出中列的名称可能已经不再重要了。

 

 

参考

spring-cache

 java.nio.buffer

美团:磁盘I/O那些事

 

posted @ 2015-09-20 16:22  等风来。。  Views(287)  Comments(0Edit  收藏  举报
------------------------------------------------------------------------------------------------------------ --------------- 欢迎联系 x.guan.ling@gmail.com--------------- ------------------------------------------------------------------------------------------------------------