0%

Linux的top和free命令里面的buffer和cache

概要

Linux的top和free命令里面的buffer和cache

博客

原帖收藏于IT老兵博客

前言

linux里面top命令和free命令都会有一个buff/cache,从08年就一直遇到这个,感觉一直没有搞的太明白,这次好好地做一个总结。
在这里插入图片描述
这个总结不见得能一步到位,到不了位,就分步来走,聚沙成塔。

正文

参考这里

buffers


Memory used by kernel buffers (Buffers in /proc/meminfo)


cache


Memory used by the page cache and slabs (Cached and SReclaimable in /proc/meminfo)


buff/cache


Sum of buffers and cache


available


Estimation of how much memory is available for starting new applications, without swapping. Unlike the data provided by the cache or free fields, this field takes into account page cache and also that not all reclaimable memory slabs will be reclaimed due to items being in use (MemAvailable in /proc/meminfo, available on kernels 3.14, emulated on kernels 2.6.27+, otherwise the same as free)

这里说,buffer是内核所使用的buffer,而cachepage cacheslabs所使用的。

内核所使用的buffer,暂时不明白。
page cache,我大概知道,是文件系统所使用的缓存。
slabs机制是内存分配的一个机制,预分配一些大块的内存,在使用的时候,再从中分配,易于回收,这样是为了规避碎片化,slab本身的意思就是大块的厚板。

上面的帖子还参考了这里,感觉讲解的更加细致。

1. What is the difference between “buffer”, and the other type of cache?


Buffers reports the amount of page cache used for block devices. The kernel has to deliberately subtract this amount from the rest of the page cache when it reports Cached.

这里说,buffer也是指的是page cache,是用于块设备的page cache

Early UNIX had a “buffer cache” of disk blocks, and did not have mmap(). Apparently when mmap() was first added, they simply bolted the page cache on top of the buffer cache. This is as messy as it sounds. Eventually, UNIX-based OS’s got rid of the buffer cache. So now all file cache is in units of pages. Pages are looked up by (file, offset), not by location on disk. This was called “unified buffer cache”, perhaps because people were more familiar with “buffer cache”.[3]

看到这段,我有点印象了,在08年的时候,那会还是2.6版本的Linux,而我正在看《情景》,《情景》是根据2.4来写的,所以,那会buffer是说用于块设备,就是这是一个数据结构,单独用作了块设备,而看上文,后来对这里进行了统一,都用统一的数据结构page来处理。

4. Why might we expect Buffers in particular to be larger or smaller?
In this case it turns out the ext4 journal size for my filesystem is 128M. So this explains why 1) my buffer cache can stabilize at slightly over 128M; 2) buffer cache does not scale proportionally with the larger amount of RAM on my laptop.

由上文可以看到,Buffer一般基本是固定的,不随着RAM的变大而变大。

For some other possible causes, see What is the buffers column in the output from free? Note that “buffers” reported by free is actually a combination of Buffers and reclaimable slab memory.

这里说buffer是包含着Buffersslab,这个地方就和上面第一篇文章矛盾了,那篇文章说的是cache包含着slab

……未完待续……(2019-10-19)

总结

参考

https://unix.stackexchange.com/questions/390518/what-do-the-buff-cache-and-avail-mem-fields-in-top-mean
http://man7.org/linux/man-pages/man1/free.1.html
https://unix.stackexchange.com/questions/261247/how-can-i-get-the-amount-of-available-memory-portably-across-distributions