怎么理解Linux内存管理中Buffer和Cache

本篇内容介绍了“怎么理解Linux内存管理中Buffer和Cache”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

在Linux中,一般使用free命令查看内存的使用情况

[root@localhost ~]# free
              total        used        free      shared  buff/cache   available
Mem:        3873760      143548     3322324        8788      407888     3453708
Swap:       1048572           0     1048572
[root@localhost ~]#

通过命令man free可查看以上输出项所代表的意思

DESCRIPTION
       free  displays  the  total amount of free and used physical and swap memory in the
       system, as well as the buffers and caches used by the kernel. The  information  is
       gathered by parsing /proc/meminfo. The displayed columns are:
       total  Total installed memory (MemTotal and SwapTotal in /proc/meminfo)
       used   Used memory (calculated as total - free - buffers - cache)
       free   Unused memory (MemFree and SwapFree in /proc/meminfo)
       shared Memory used (mostly) by tmpfs (Shmem in /proc/meminfo, available on kernels
              2.6.32, displayed as zero if not available)
       buffers
              Memory used by kernel buffers (Buffers in /proc/meminfo)
       cache  Memory used by the page cache and slabs (Cached and Slab 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+, oth‐
              erwise the same as free)

free命令显示了空闲+物理使用+交换区等信息,通过解析/proc/meminfo获得,该文件中的内容如下:

[root@localhost ~]# cat /proc/meminfo
MemTotal:        3873760 kB
MemFree:         3322052 kB
MemAvailable:    3453436 kB
Buffers:            3252 kB
Cached:           320316 kB
SwapCached:            0 kB
Active:           256968 kB
Inactive:         124468 kB
Active(anon):      58264 kB
Inactive(anon):     8392 kB
Active(file):     198704 kB
Inactive(file):   116076 kB
Unevictable:           0 kB
Mlocked:               0 kB
SwapTotal:       1048572 kB
SwapFree:        1048572 kB
Dirty:                 0 kB
Writeback:             0 kB
AnonPages:         57868 kB
Mapped:            27384 kB
Shmem:              8788 kB
Slab:              84320 kB
SReclaimable:      42580 kB
SUnreclaim:        41740 kB
KernelStack:        3120 kB
PageTables:         4492 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:     2985452 kB
Committed_AS:     293568 kB
VmallocTotal:   34359738367 kB
VmallocUsed:      180372 kB
VmallocChunk:   34359326716 kB
HardwareCorrupted:     0 kB
AnonHugePages:      4096 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:       81728 kB
DirectMap2M:     3063808 kB
DirectMap1G:     3145728 kB
[root@localhost ~]#

total - 总内存大小
used - 使用的内存大小
free - 未使用内存大小
shared - tmpfs使用的内存
buffers - 内核缓存大小(/proc/meminfo中的Buffers)
cache - 页面缓存和slabs(/proc/meminfo中的Cached和Slab)
buff/cache - buffers + cache
available - 可用内存大小(剔除交换区)

buffers和cache在man free中仍没有详细解释清楚,通过查看kernel doc进一步获得相关的解释

...
Buffers: Relatively temporary storage for raw disk blocks
         shouldn't get tremendously large (20MB or so)
Cached: in-memory cache for files read from the disk (the
        pagecache).  Doesn't include SwapCached
Slab: in-kernel data structures cache
SReclaimable: Part of Slab, that might be reclaimed, such as caches
SUnreclaim: Part of Slab, that cannot be reclaimed on memory pressure

Buffers - 裸磁盘块的临时存储(通常不会不大,20MB左右)
Cached - 从磁盘上读取文件的缓存(即页面缓存).不包括SwapCached
Slab - 内核中的数据结构缓存
SReclaimable - Slab中的一部分,可回收
SUnreclaim - Slab中的另一部分,不能回收

使用vmstat可监控内存的使用情况

[root@localhost ~]# echo 3 > /proc/sys/vm/drop_caches
[root@localhost ~]# vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  1      0 3648060      0  96964    0    0     4  5158  151  174  5  6 89  0  0
 0  0      0 3648248      0  97424    0    0   296     0  311  331  0  1 99  0  0
...

“怎么理解Linux内存管理中Buffer和Cache”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!