• 内存中的buffer与cache


    目录

    buffer与cache

    1、free命令

    2、proc系统中参数说明

    试验一 :文件与磁盘的写

    1、文件写入

    2、磁盘写入

    试验二 :文件和磁盘的读

    1、文件读取

    2、读磁盘 

    总结


    buffer与cache

    命令行中查看内存中的buffer与cache,并跟踪理解其含义

    1、free命令

    1. root@wy-virtual-machine:~# free
    2. total used free shared buff/cache available
    3. Mem: 8107544 1050180 5182000 37764 1875364 6760616
    4. Swap: 2097148 0 2097148

    free命令输出的结果有总内存,已用内存,空闲内存,缓存,可用内存。缓存是buffer与cache的总和。从字面意思上理解,buffer是缓冲区,cache是缓存,都是内存中的临时存储区,两者的区别是什么呢?

    在free的使用说明中有以下说明

    1. #man free
    2. buffers
    3. Memory used by kernel buffers (Buffers in /proc/meminfo)
    4. cache Memory used by the page cache and slabs (Cached and SReclaimable in /proc/meminfo)
    5. buff/cache
    6. Sum of buffers and cache

    buffer 是内核缓冲区使用的内存,对应的/proc/meminfo中的Buffer值

    cache是页缓冲区和slab 使用的内存,对应的是/proc/meminfo中的Cached 与 SReclaimable的和

    2、proc系统中参数说明

    具体还要看proc系统中的参数含义

    1. # man proc
    2. ...
    3. Buffers %lu
    4. Relatively temporary storage for raw disk blocks that shouldn't get tremendously large (20 MB
    5. or so).
    6. Cached %lu
    7. In-memory cache for files read from the disk (the page cache). Doesn't include SwapCached.
    8. SReclaimable %lu (since Linux 2.6.19)
    9. Part of Slab, that might be reclaimed, such as caches.

    Buffers:关于原始磁盘块的临时存储,通常不会特别大(20MB左右,内存集中,统一写入)

    Cached:从磁盘读取文件的页缓存,不包括SwapCached

    SReclaimable:是Slab的一部分,也许能被回收,如caches

    下面用试验来深入理解buffer与cache的不同


    试验一 :文件与磁盘的写

    1、文件写入

    终端1 写文件       

    dd if=/dev/urandom of=/tmp/file bs=1M count=500

    中端2  使用vmstat观察内存,每1s输出一次

    1. root@wy-virtual-machine:~# vmstat 1
    2. procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
    3. r b swpd free buff cache si so bi bo in cs us sy id wa st
    4. 0 0 0 6651680 3880 375752 0 0 85 79 295 903 2 2 96 0 0
    5. 0 0 0 6651680 3880 375752 0 0 0 0 249 478 0 1 99 0 0
    6. 0 0 0 6651680 3880 375752 0 0 0 0 676 2261 5 4 92 0 0
    7. 0 0 0 6651680 3880 375756 0 0 0 20 246 635 0 1 99 0 0
    8. 0 0 0 6651680 3880 375752 0 0 0 0 195 484 1 0 99 0 0
    9. 1 0 0 6582632 4192 457268 0 0 380 0 284 477 0 18 82 0 0
    10. 1 0 0 6337940 4192 701892 0 0 0 0 436 291 0 51 49 0 0
    11. 1 0 0 6138868 4228 900584 0 0 32 319488 856 445 0 50 50 0 0
    12. 0 0 0 6138868 4240 900624 0 0 16 192512 397 457 0 6 93 1 0
    13. 0 0 0 6138868 4240 900628 0 0 0 0 128 258 1 0 99 1 0

    试验现象

    在申请内存阶段,cache迅速增长,而buffer基本不变。

    写的数据量bi与bo的数据相加 (319488KB + 192512KB + 32KB+16KB)/1024 = 500MB(约等于)

    2、磁盘写入

    终端1 磁盘写入        

    1. (base) root@ubuntu:~# dd if=/dev/urandom of=/dev/sdc1 bs=1M count=500
    2. 500+0 records in
    3. 500+0 records out
    4. 524288000 bytes (524 MB, 500 MiB) copied, 3.93891 s, 133 MB/s

    终端2 vmstat 1 观察

    1. procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
    2. r b swpd free buff cache si so bi bo in cs us sy id wa st
    3. 0 0 0 2297680 5124 348260 0 0 0 0 164 263 0 0 100 0 0
    4. 0 0 0 2297680 5124 348260 0 0 0 0 224 309 0 0 93 7 0
    5. 0 0 0 2297680 5124 348260 0 0 0 0 154 254 0 0 100 0 0
    6. 0 1 0 2297680 5124 348260 0 0 0 0 153 256 0 0 99 1 0
    7. 0 0 0 2297680 5124 348260 0 0 0 0 139 250 0 0 94 6 0
    8. 0 0 0 2297680 5124 348260 0 0 0 0 105 206 0 0 100 0 0
    9. 1 0 0 2101664 167392 381136 0 0 0 0 658 552 0 24 70 7 0
    10. 1 1 0 1960788 284280 405276 0 0 0 6240 859 989 0 26 66 8 0
    11. 1 1 0 1821180 400388 428932 0 0 0 15840 1325 1826 0 26 49 25 0
    12. 0 2 0 1679836 517808 453016 0 0 684 16560 1255 1874 0 25 50 25 0
    13. 2 2 0 1678508 518324 453136 0 0 584 13680 1452 2028 1 0 71 29 0
    14. 0 1 0 1678508 518324 453136 0 0 0 13324 2153 2849 0 1 74 25 0
    15. 0 1 0 1678508 518324 453136 0 0 0 8760 1278 1892 1 2 74 24 0
    16. 0 1 0 1678508 518324 453136 0 0 0 15720 2240 3168 0 1 74 24 0
    17. 0 1 0 1678508 518324 453136 0 0 0 16440 1522 2857 0 1 74 25 0

    现象:写磁盘,buffer 增加的多

    总结,写文件用到cache缓存数据,写磁盘用到buffer的缓存数据

    试验二 :文件和磁盘的读

    1、文件读取

    终端1中执行:  

    dd if=/tmp/file of=/dev/null

    终端2执行 vmstat 1如下

    1. root@ubuntu:/# vmstat 1
    2. procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
    3. r b swpd free buff cache si so bi bo in cs us sy id wa st
    4. 0 0 0 2285628 3764 349756 0 0 0 92 193 250 0 0 93 7 0
    5. 0 0 0 2285628 3764 349756 0 0 0 0 279 460 0 0 100 0 0
    6. 1 0 0 1778668 4028 857032 0 0 507588 0 8483 617 0 22 78 0 0
    7. 1 0 0 1773132 4092 861840 0 0 4820 0 1108 1907 1 1 98 0 0

    cache 迅速增长,buffer基本不变,bi 变化比较大 

    bi的变化 (507588 + 4820)/1024 = 500.3984375

    2、读磁盘 

    终端1 执行

    dd if=/dev/sdc1 of=/dev/null bs=1M count=500

    终端2执行 vmstat 1如下

    1. root@ubuntu:/# vmstat 1
    2. procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
    3. r b swpd free buff cache si so bi bo in cs us sy id wa st
    4. 1 1 0 2287180 2280 349444 0 0 502 306 123 214 0 1 94 4 0
    5. 0 0 0 2286676 2980 349464 0 0 700 0 360 634 0 0 97 2 0
    6. 0 0 0 2286424 3288 349376 0 0 308 0 291 505 0 0 99 0 0
    7. 0 0 0 2286424 3600 349480 0 0 328 0 1680 2850 1 2 90 7 0
    8. 0 1 0 2283652 5528 349532 0 0 2004 0 415 739 0 1 96 3 0
    9. 0 1 0 2272304 17060 349556 0 0 11524 24 1170 2359 0 0 68 32 0
    10. 1 1 0 2260712 28580 349560 0 0 11524 0 1180 2351 0 0 75 25 0
    11. 0 1 0 2248868 40236 349560 0 0 11656 0 1159 2348 0 0 75 25 0
    12. 0 1 0 2237528 51756 349584 0 0 11520 0 1077 2253 0 1 75 25 0
    13. 0 2 0 2225892 63404 349560 0 0 11648 0 2325 3137 0 1 73 27 0
    14. 0 1 0 2214552 74924 349620 0 0 11520 0 1769 2338 0 1 70 29 0
    15. 0 1 0 2203968 86572 349536 0 0 11648 0 1359 2195 0 0 75 25 0
    16. 1 1 0 2192628 98092 349560 0 0 11520 0 1370 2301 0 0 75 25 0
    17. 1 1 0 2181036 109740 349560 0 0 11648 0 1341 2276 0 0 75 25 0
    18. 1 2 0 2169444 121260 349560 0 0 11520 0 1550 2305 0 1 75 25 0
    19. 0 1 0 2157852 132908 349560 0 0 11648 0 1740 2390 0 1 74 24 0
    20. 0 2 0 2147268 144556 349560 0 0 11648 0 1536 2226 0 1 69 31 0
    21. 0 1 0 2135424 156204 349560 0 0 11648 0 1343 2111 0 0 74 26 0
    22. 0 1 0 2124084 167724 349560 0 0 11520 0 1303 2045 0 0 75 25 0
    23. 0 1 0 2112492 179244 349560 0 0 11520 0 1347 2123 0 0 68 32 0
    24. 0 1 0 2100900 190892 349512 0 0 11648 0 1321 2072 0 0 75 25 0
    25. 0 1 0 2089308 202412 349560 0 0 11520 0 1329 2094 0 0 68 32 0
    26. 0 1 0 2077716 214060 349560 0 0 11648 0 1315 2064 0 0 75 25 0
    27. 0 1 0 2066124 225580 349560 0 0 11520 0 1334 2099 0 0 68 32 0
    28. 0 1 0 2054532 237228 349468 0 0 11664 0 1641 3268 0 1 75 24 0
    29. 1 1 0 2043192 248748 349636 0 0 11648 0 814 2333 0 0 75 24 0
    30. 0 1 0 2031348 260396 349576 0 0 11520 0 770 2233 0 0 76 24 0
    31. 0 1 0 2019756 272044 349580 0 0 11652 0 825 2285 0 0 75 25 0
    32. procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
    33. r b swpd free buff cache si so bi bo in cs us sy id wa st
    34. 2 0 0 2008416 283564 349580 0 0 11648 0 785 2255 0 0 75 24 0
    35. 0 1 0 1996572 295212 349532 0 0 11520 0 770 2265 0 0 75 25 0
    36. 0 1 0 1984980 306860 349580 0 0 11648 76 819 2299 0 1 75 25 0
    37. 0 1 0 1973388 318380 349580 0 0 11520 0 766 2254 0 0 75 25 0
    38. 0 1 0 1961796 330028 349548 0 0 11648 0 799 2287 0 0 75 24 0
    39. 0 1 0 1950204 341548 349580 0 0 11520 0 1041 2517 1 1 74 25 0
    40. 1 2 0 1938612 353196 349612 0 0 11680 0 2072 4171 1 2 73 24 0
    41. 0 1 0 1927020 364852 349508 0 0 11648 12 1446 3271 0 1 74 24 0
    42. 0 1 0 1915176 376500 349616 0 0 11648 0 892 2330 0 1 75 24 0
    43. 0 1 0 1903584 388020 349596 0 0 11520 0 1952 4150 0 2 73 25 0
    44. 3 0 0 1891740 399668 349508 0 0 11648 0 2025 4479 1 2 74 23 0
    45. 0 1 0 1880400 411316 349612 0 0 11648 0 1846 3563 1 1 67 31 0
    46. 0 1 0 1868556 422964 349612 0 0 11648 20 1155 2284 0 0 75 25 0
    47. 0 1 0 1856964 434612 349612 0 0 11648 0 1425 2870 1 0 68 32 0
    48. 0 1 0 1845372 446132 349612 0 0 11520 0 1290 2548 0 0 75 25 0
    49. 0 2 0 1833780 457780 349612 0 0 11648 0 1338 2807 0 0 75 25 0
    50. 0 1 0 1822440 469300 349612 0 0 11520 0 1342 3344 1 1 75 24 0
    51. 0 1 0 1810596 480948 349612 0 0 11648 0 1227 2864 1 0 75 24 0
    52. 0 1 0 1798752 492596 349564 0 0 11648 0 873 2350 0 0 75 25 0
    53. 0 1 0 1787412 504116 349612 0 0 11520 0 864 2340 0 0 75 24 0
    54. 0 1 0 1775568 515764 349612 0 0 11648 0 1755 2354 0 0 75 25 0
    55. 0 0 0 1775064 516024 349616 0 0 264 0 1230 2043 0 1 98 1 0
    56. 0 1 0 1775064 516024 349616 0 0 0 0 910 1524 0 0 95 5 0
    57. 0 0 0 1775064 516024 349616 0 0 0 0 317 524 0 0 98 2 0
    58. 0 0 0 1775064 516024 349616 0 0 0 0 136 219 0 1 100 0 0

    buffer 在增长,bi 在变化 说明读磁盘文件时,数据缓存到了buffer缓冲区中

    读磁盘的速度与文件相比,明显慢很多。

    总结

            Buffer 可以用作将数据写入磁盘的缓存,也可以用作输出从磁盘读取的缓存

            Cache 可以用作文件读取数据的页缓存,也可用作写文件作页的缓存

    参考链接

    https://course.0voice.com/v1/course/intro?courseId=2&agentId=0


  • 相关阅读:
    Linux安装CUDA & 添加环境变量 & 多版本CUDA切换 (软链接)
    Spring IoC 容器生命周期:Ioc容器启停过程发生了什么-13
    C++基础——函数
    刷题分享—常见的经典面试题二
    python中matrix()矩阵和array()数组(待完善)
    【BMS软开系列】1、 ISO 26262功能安全标准 (一)
    软件测试人员提问常用的ChatGPT通用提示词模板
    echarts折线图让某一个点闪烁,echarts闪烁动画。
    200行C++代码写一个Qt俄罗斯方块小游戏
    邮件安全不容忽视,教你如何防止邮件泄密!
  • 原文地址:https://blog.csdn.net/WANGYONGZIXUE/article/details/126200170