前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >关于 linux 操作系统中的 buff/cache

关于 linux 操作系统中的 buff/cache

作者头像
程序员架构进阶
发布2021-03-05 10:42:46
1.9K0
发布2021-03-05 10:42:46
举报
文章被收录于专栏:架构进阶

一 背景

如图,当我们查看内存信息时,通常会使用vmstat或free命令。在使用vmstat -S M时,会看到下面的结果。

free -m:

在这里,我们能够看到内存信息中包含了swpd, free, buff, cache等等。其中,最熟悉和分析最多的就是buff 和 cache。通常,我们都有简单的了解,例如buffer是缓冲区,cache是缓存;通常操作时是读cache,写buffer等等,但深入一点,这二者的差别是什么呢?本章将结合stackoverflow上的一个问题及回答进行分析。

二 解答

2.1 根据以往资料整理的理解

1、Buffer(缓冲区)是系统两端处理速度平衡(从长时间尺度上看)时使用的。它的引入是为了减小短期内突发I/O的影响,起到流量整形的作用。比如生产者——消费者问题,他们产生和消耗资源的速度大体接近,加一个buffer可以抵消掉资源刚产生/消耗时的突然变化。

2、Cache(缓存)则是系统两端处理速度不匹配时的一种折衷策略。因为CPU和memory之间的速度差异越来越大,所以人们充分利用数据的局部性(locality)特征,通过使用存储系统分级(memory hierarchy)的策略来减小这种差异带来的影响。

3、假定以后存储器访问变得跟CPU做计算一样快,cache就可以消失,但是buffer依然存在。比如从网络上下载东西,瞬时速率可能会有较大变化,但从长期来看却是稳定的,这样就能通过引入一个buffer使得OS接收数据的速率更稳定,进一步减少对磁盘的伤害。

2.2 stackoverflow的回答

原问题:https://stackoverflow.com/questions/6345020/what-is-the-difference-between-buffer-and-cache-memory-in-linux

Linux中,buff和cache 内存有什么区别?

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 and Buffers is raw disk blocks not represented in the page cache—i.e., not file data. The Buffers metric is thus of minimal importance. On most systems, Buffers is often only tens of megabytes.

翻译过来:

简短回答:Cached是页缓存(page cache)的大小;而Buffers是内存块I/O缓冲区的大小。Cache很重要,而Buffers没那么重要。

详细回答:Cached是Linux页缓存的大小减去swap cache(交换区)中内存的大小——SwapCached(全部页缓存大小等于Cached+SwapCached)。Linux通过页缓存来执行所有文件I/O操作。写操作只是简单地将页缓存中的相应页标记为脏页。读操作是通过返回页缓存中的数据来实现的;如果数据还没有在缓存中,会先添加。在现在的Linux操作系统中,Cached很容易达到GB级别,它只会在内存面临压力时缩小。系统将清除页面缓存,同时将内存中的数据交换到磁盘,以便在需要时提供更多可用内存。

Buffers(缓冲区)是内存块 I/O 缓冲区,生命周期相对较短。在Linux内核版本2.4之前,Linux有独立的页缓存和缓冲区缓存。从2.4版本开始,页缓存和缓冲区缓存统一,缓冲区是未展现在页缓存中的原始(裸)磁盘块——也就是说,不是文件数据。因此缓冲区的大小并不重要,在大部分系统中,缓冲区通常只有几十M字节。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-11-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 程序员架构进阶 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一 背景
  • 二 解答
    • 2.1 根据以往资料整理的理解
      • 2.2 stackoverflow的回答
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档