Linux 中的缓冲区与高速缓存有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6345020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:31:31  来源:igfitidea点击:

What is the difference between buffer vs cache memory in Linux?

linuxcachingmemorybuffer

提问by James.Xu

To me it's not clear what's the difference between the two Linux memory concept :bufferand cache. I've read through this postand it seems to me that the difference between them is the expiration policy:

对我来说,不清楚两个 Linux 内存概念之间的区别是什么:buffercache. 我已经阅读了这篇文章,在我看来,它们之间的区别在于到期政策:

  1. buffer's policy is first-in, first-out
  2. cache's policy is Least Recently Used.
  1. 缓冲区的策略是先进先出
  2. 缓存的策略是最近最少使用。

Am I right?

我对吗?

In particular, I am looking at the two commands: freeand vmstat

特别是,我正在查看两个命令:freevmstat

james@utopia:~$ vmstat -S M
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
5  0      0    173     67    912    0    0    19    59   75 1087 24  4 71  1
james@utopia:~$ free -m
             total       used       free     shared    buffers     cached
Mem:          2007       1834        172          0         67        914
-/+ buffers/cache:        853       1153
Swap:         2859          0       2859

采纳答案by Seth Robertson

"Buffers" represent how much portion of RAM is dedicated to cache disk blocks. "Cached" is similar like "Buffers", only this time it caches pages from file reading.

“缓冲区”表示有多少 RAM 部分专用于缓存磁盘块。“Cached”类似于“Buffers”,只是这次它从文件读取中缓存页面。

quote from:

引自:

回答by Abigail

A buffer is a region of memory used to temporarily hold data while it is being moved from one place to another within a computer.while a cache is a temporary storage area where frequently accessed data can be stored for rapid access. Once the data is stored in the cache, future use can be made by accessing the cached copy rather than re-fetching the original data, so that the average access time is shorter.

缓冲区是用于在计算机内将数据从一个位置移动到另一个位置时临时保存数据的内存区域。而缓存是一个临时存储区域,可以在其中存储经常访问的数据以便快速访问。一旦数据存储在缓存中,以后可以通过访问缓存的副本而不是重新获取原始数据来使用,从而缩短平均访问时间。

回答by ChaiZhi

buffer and cache.

缓冲区和缓存。

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.

缓存是从磁盘“读取”并存储以备后用的东西。

回答by xoy

Buffers are associated with a specific block device, and cover caching of filesystem metadata as well as tracking in-flight pages. The cache only contains parked file data. That is, the buffers remember what's in directories, what file permissions are, and keep track of what memory is being written from or read to for a particular block device. The cache only contains the contents of the files themselves.

缓冲区与特定的块设备相关联,涵盖文件系统元数据的缓存以及跟踪动态页面。缓存仅包含停放的文件数据。也就是说,缓冲区记住目录中的内容,文件权限是什么,并跟踪特定块设备正在写入或读取的内存。缓存只包含文件本身的内容。

quote link

报价链接

回答by socketpair

Cited answer (for reference):

引用答案(供参考):

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.

简短回答:缓存是页面缓存的大小。Buffers 是内存块 I/O 缓冲区的大小。缓存事项;缓冲区在很大程度上是无关紧要的。

长答案:Cached 是 Linux 页面缓存的大小,减去交换缓存中的内存,用 SwapCached 表示(因此总页面缓存大小为 Cached + SwapCached)。Linux 通过页面缓存执行所有文件 I/O。写被实现为简单地将页缓存中的相应页标记为脏页;然后刷新线程定期将任何脏页写回磁盘。读取是通过从页缓存返回数据来实现的;如果数据还没有在缓存中,它首先被填充。在现代 Linux 系统上,Cached 很容易达到几 GB。它只会因内存压力而缩小。系统将清除页面缓存并将数据交换到磁盘以根据需要提供更多内存。

缓冲区是内存中的块 I/O 缓冲区。它们的寿命相对较短。在 Linux 内核版本 2.4 之前,Linux 具有单独的页面和缓冲区缓存。从 2.4 开始,页面缓存和缓冲区缓存是统一的,缓冲区是页面缓存中未表示的原始磁盘块,即不是文件数据。因此,缓冲区指标的重要性最低。在大多数系统上,Buffers 通常只有几十兆字节。

回答by n00ber

It's not 'quite' as simple as this, but it might help understand:

它不像这样“非常”简单,但它可能有助于理解:

Buffer is for storing file metadata (permissions, location, etc). Every memory page is kept track of here.

缓冲区用于存储文件元数据(权限、位置等)。每一个内存页都被记录在这里。

Cache is for storing actual file contents.

缓存用于存储实际文件内容。

回答by Eric

I think this page will help understanding the difference between buffer and cache deeply. http://www.tldp.org/LDP/sag/html/buffer-cache.html

我认为这个页面将有助于深入理解缓冲区和缓存之间的区别。http://www.tldp.org/LDP/sag/html/buffer-cache.html

Reading from a disk is very slow compared to accessing (real) memory. In addition, it is common to read the same part of a disk several times during relatively short periods of time. For example, one might first read an e-mail message, then read the letter into an editor when replying to it, then make the mail program read it again when copying it to a folder. Or, consider how often the command lsmight be run on a system with many users. By reading the information from disk only once and then keeping it in memory until no longer needed, one can speed up all but the first read. This is called disk buffering, and the memory used for the purpose is called the buffer cache.

与访问(真实)内存相比,从磁盘读取非常慢。此外,在相对较短的时间内多次读取磁盘的同一部分是很常见的。例如,人们可能首先阅读一封电子邮件,然后在回复时将信件读入编辑器,然后在将其复制到文件夹时让邮件程序再次阅读。或者,考虑ls在具有许多用户的系统上运行该命令的频率。通过只从磁盘读取一次信息,然后将其保存在内存中直到不再需要,可以加快除第一次读取之外的所有操作。这称为磁盘缓冲,用于此目的的内存称为缓冲区缓存。

Since memory is, unfortunately, a finite, nay, scarce resource, the buffer cache usually cannot be big enough (it can't hold all the data one ever wants to use). When the cache fills up, the data that has been unused for the longest time is discarded and the memory thus freed is used for the new data.

不幸的是,由于内存是一种有限的稀缺资源,因此缓冲区缓存通常不能足够大(它无法容纳人们想要使用的所有数据)。当缓存填满时,最长时间未使用的数据将被丢弃,从而释放的内存用于新数据。

Disk buffering works for writes as well. On the one hand, data that is written is often soon read again (e.g., a source code file is saved to a file, then read by the compiler), so putting data that is written in the cache is a good idea. On the other hand, by only putting the data into the cache, not writing it to disk at once, the program that writes runs quicker. The writes can then be done in the background, without slowing down the other programs.

磁盘缓冲也适用于写入。一方面,写入的数据通常很快就会再次读取(例如,将源代码文件保存到文件中,然后由编译器读取),因此将写入的数据放入缓存中是一个好主意。另一方面,通过只将数据放入缓存,而不是立即将其写入磁盘,写入的程序运行得更快。然后可以在后台完成写入,而不会减慢其他程序的速度。

回答by karthik

Buffer contains metadata which helps improve write performance

缓冲区包含有助于提高写入性能的元数据

Cache contains the file content itself (sometimes yet to write to disk) which improves read performance

缓存包含文件内容本身(有时尚未写入磁盘),从而提高读取性能

回答by Chao Yin

Seth Robertson's Link 2 said "For thorough understanding of those terms, refer to Linux kernel book like Linux Kernel Development by Robert M. Love."

Seth Robertson 的 Link 2 说“要彻底理解这些术语,请参阅 Linux 内核书籍,例如 Robert M. Love 的 Linux Kernel Development”。

I found some contents about 'buffer' in the 2nd edition of the book.

我在本书的第二版中找到了一些关于“缓冲”的内容。

Although the physical device itself is addressable at the sector level, the kernel performs all disk operations in terms of blocks.

When a block is stored in memory (say, after a read or pending a write), it is stored in a 'buffer'. Each 'buffer' is associated with exactly one block. The 'buffer' serves as the object that represents a disk block in memory.

A 'buffer' is the in-memory representation of a single physical disk block.

Block I/O operations manipulate a single disk block at a time. A common block I/O operation is reading and writing inodes. The kernel provides the bread() function to perform a low-level read of a single block from disk. Via 'buffers', disk blocks are mapped to their associated in-memory pages. "

尽管物理设备本身可以在扇区级别进行寻址,但内核以块的形式执行所有磁盘操作。

当一个块存储在内存中时(例如,在读取或挂起写入之后),它存储在“缓冲区”中。每个“缓冲区”都与一个块相关联。“缓冲区”用作表示内存中磁盘块的对象。

“缓冲区”是单个物理磁盘块的内存表示。

块 I/O 操作一次操作一个磁盘块。一个常见的块 I/O 操作是读取和写入 inode。内核提供了bread() 函数来执行从磁盘的单个块的低级读取。通过“缓冲区”,磁盘块被映射到它们相关的内存页面。”

回答by Ijaz Ahmad Khan

Explained by RedHat:

RedHat解释:

Cache Pages:

缓存页面:

A cache is the part of the memory which transparently stores data so that future requests for that data can be served faster. This memory is utilized by the kernel to cache disk data and improve i/o performance.

缓存是内存的一部分,它透明地存储数据,以便可以更快地处理未来对该数据的请求。内核使用此内存来缓存磁盘数据并提高 I/O 性能。

The Linux kernel is built in such a way that it will use as much RAM as it can to cache information from your local and remote filesystems and disks. As the time passes over various reads and writes are performed on the system, kernel tries to keep data stored in the memory for the various processes which are running on the system or the data that of relevant processes which would be used in the near future. The cache is not reclaimed at the time when process get stop/exit, however when the other processes requires more memory then the free available memory, kernel will run heuristics to reclaim the memory by storing the cache data and allocating that memory to new process.

Linux 内核的构建方式将使用尽可能多的 RAM 来缓存来自本地和远程文件系统和磁盘的信息。随着时间的流逝,在系统上执行各种读取和写入操作时,内核会尝试将存储在内存中的数据保存在系统上运行的各个进程的内存中,或者将在不久的将来使用的相关进程的数据。在进程停止/退出时不会回收缓存,但是当其他进程需要比可用内存更多的内存时,内核将运行试探法,通过存储缓存数据并将该内存分配给新进程来回收内存。

When any kind of file/data is requested then the kernel will look for a copy of the part of the file the user is acting on, and, if no such copy exists, it will allocate one new page of cache memory and fill it with the appropriate contents read out from the disk.

当请求任何类型的文件/数据时,内核将查找用户正在处理的文件部分的副本,如果不存在这样的副本,它将分配一个新的高速缓存页面并用从磁盘中读出相应的内容。

The data that is stored within a cache might be values that have been computed earlier or duplicates of original values that are stored elsewhere in the disk. When some data is requested, the cache is first checked to see whether it contains that data. The data can be retrieved more quickly from the cache than from its source origin.

存储在缓存中的数据可能是之前计算过的值,也可能是存储在磁盘其他地方的原始值的副本。当请求某些数据时,首先检查缓存以查看它是否包含该数据。从缓存中检索数据的速度比从其源来源检索的速度更快。

SysV shared memory segments are also accounted as a cache, though they do not represent any data on the disks. One can check the size of the shared memory segments using ipcs -m command and checking the bytes column.

SysV 共享内存段也被视为缓存,尽管它们不代表磁盘上的任何数据。可以使用 ipcs -m 命令并检查字节列来检查共享内存段的大小。

Buffers :

缓冲器:

Buffers are the disk block representation of the data that is stored under the page caches. Buffers contains the metadata of the files/data which resides under the page cache. Example: When there is a request of any data which is present in the page cache, first the kernel checks the data in the buffers which contain the metadata which points to the actual files/data contained in the page caches. Once from the metadata the actual block address of the file is known, it is picked up by the kernel for processing.

缓冲区是存储在页面缓存下的数据的磁盘块表示。缓冲区包含驻留在页面缓存下的文件/数据的元数据。示例:当请求页面缓存中存在的任何数据时,内核首先检查缓冲区中的数据,其中包含指向页面缓存中包含的实际文件/数据的元数据。一旦从元数据中知道了文件的实际块地址,内核就会将其提取出来进行处理。