千家信息网

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

发表于:2025-01-19 作者:千家信息网编辑
千家信息网最后更新 2025年01月19日,本篇内容介绍了"怎么理解Linux内存管理中Buffer和Cache"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,
千家信息网最后更新 2025年01月19日怎么理解Linux内存管理中Buffer和Cache

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

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

[root@localhost ~]# free              total        used        free      shared  buff/cache   availableMem:        3873760      143548     3322324        8788      407888     3453708Swap:       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/meminfoMemTotal:        3873760 kBMemFree:         3322052 kBMemAvailable:    3453436 kBBuffers:            3252 kBCached:           320316 kBSwapCached:            0 kBActive:           256968 kBInactive:         124468 kBActive(anon):      58264 kBInactive(anon):     8392 kBActive(file):     198704 kBInactive(file):   116076 kBUnevictable:           0 kBMlocked:               0 kBSwapTotal:       1048572 kBSwapFree:        1048572 kBDirty:                 0 kBWriteback:             0 kBAnonPages:         57868 kBMapped:            27384 kBShmem:              8788 kBSlab:              84320 kBSReclaimable:      42580 kBSUnreclaim:        41740 kBKernelStack:        3120 kBPageTables:         4492 kBNFS_Unstable:          0 kBBounce:                0 kBWritebackTmp:          0 kBCommitLimit:     2985452 kBCommitted_AS:     293568 kBVmallocTotal:   34359738367 kBVmallocUsed:      180372 kBVmallocChunk:   34359326716 kBHardwareCorrupted:     0 kBAnonHugePages:      4096 kBHugePages_Total:       0HugePages_Free:        0HugePages_Rsvd:        0HugePages_Surp:        0Hugepagesize:       2048 kBDirectMap4k:       81728 kBDirectMap2M:     3063808 kBDirectMap1G:     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 SwapCachedSlab: in-kernel data structures cacheSReclaimable: Part of Slab, that might be reclaimed, such as cachesSUnreclaim: 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 1procs -----------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"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

0