溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么理解Linux內(nèi)存管理中Buffer和Cache

發(fā)布時(shí)間:2021-11-08 16:16:33 來(lái)源:億速云 閱讀:168 作者:iii 欄目:關(guān)系型數(shù)據(jù)庫(kù)

本篇內(nèi)容介紹了“怎么理解Linux內(nèi)存管理中Buffer和Cache”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

在Linux中,一般使用free命令查看內(nèi)存的使用情況

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

通過(guò)命令man free可查看以上輸出項(xiàng)所代表的意思

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命令顯示了空閑+物理使用+交換區(qū)等信息,通過(guò)解析/proc/meminfo獲得,該文件中的內(nèi)容如下:

[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 - 總內(nèi)存大小
used - 使用的內(nèi)存大小
free - 未使用內(nèi)存大小
shared - tmpfs使用的內(nèi)存
buffers - 內(nèi)核緩存大小(/proc/meminfo中的Buffers)
cache - 頁(yè)面緩存和slabs(/proc/meminfo中的Cached和Slab)
buff/cache - buffers + cache
available - 可用內(nèi)存大小(剔除交換區(qū))

buffers和cache在man free中仍沒(méi)有詳細(xì)解釋清楚,通過(guò)查看kernel doc進(jìn)一步獲得相關(guān)的解釋

...
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 - 裸磁盤(pán)塊的臨時(shí)存儲(chǔ)(通常不會(huì)不大,20MB左右)
Cached - 從磁盤(pán)上讀取文件的緩存(即頁(yè)面緩存).不包括SwapCached
Slab - 內(nèi)核中的數(shù)據(jù)結(jié)構(gòu)緩存
SReclaimable - Slab中的一部分,可回收
SUnreclaim - Slab中的另一部分,不能回收

使用vmstat可監(jiān)控內(nèi)存的使用情況

[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內(nèi)存管理中Buffer和Cache”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI