Coder Perfect

In Linux, what’s the difference between buffer and cache memory?

Problem

To me, the distinction between the two Linux memory notions of buffer and cache is unclear. I’ve looked through this post and the difference between them appears to be the expiration policy:

Am I right?

I’m looking at two commands in particular: free and vmstat.

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

Asked by James.Xu

Solution #1

quote link

Answered by xoy

Solution #2

Answer cited (as a point of reference):

Answered by socketpair

Solution #3

The term “buffers” refers to the amount of RAM dedicated to caching disc chunks. “Cached” is similar to “Buffers,” only it caches pages from file reading this time.

quote from:

Answered by Seth Robertson

Solution #4

It’s not exactly as straightforward as this, but it might help:

The buffer is used to store file metadata (permissions, location, etc). This is where every memory page is kept track of.

The cache is used to save the contents of files.

Answered by n00ber

Solution #5

Red Hat explains it this way:

Cache Pages:

A cache is a section of memory that saves data invisibly so that subsequent requests for that data can be served more quickly. The kernel makes use of this RAM to cache disk data and increase i/o performance.

The Linux kernel is designed to cache information from your local and remote filesystems and disks using as much RAM as possible. As time passes, numerous reads and writes are conducted on the system, and the kernel strives to keep data stored in memory for the various processes that are currently operating on the system, as well as data for relevant processes that will be utilized in the near future. When a process stops or exits, the cache is not reclaimed; however, if other processes require more memory than the free memory, the kernel will use heuristics to reclaim the memory by saving cache data and assigning that memory to a new process.

If a copy of the part of the file the user is acting on does not exist, the kernel will allocate one new page of cache memory and fill it with the relevant contents read from the disk.

Values that have been computed before or duplicates of original values that are saved elsewhere on the disk may be stored in a cache. When data is requested, the cache is initially examined to see if it contains the desired 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.

Buffers:

The data that is held under the page caches is represented by buffers, which are disk blocks. The metadata of the files/data that sit behind the page cache is stored in buffers. When a request for data from the page cache is made, the kernel first verifies the data in the buffers that hold the metadata that leads to the actual files/data stored in the page caches. The kernel picks up the file for processing once the actual block address of the file is known from the metadata.

Answered by Ijaz Ahmad

Post is based on https://stackoverflow.com/questions/6345020/what-is-the-difference-between-buffer-and-cache-memory-in-linux