关于内存free转换到buffer/cache之后,内存被用完的解决思路
最近跑程序,发现linux在执行大量读写操作后,内存的可用(free)会不断被buffer/cache所占据,导致内存空间被用完,一直以为是代码哪里写的问题,导致内存泄露,后来发现就是发生了I/O读写操作后,会产生buffer/cache,需要定时释放。
这个情况也是第一次遇到,不知道如何解决
问了chatgpt是这么解释的:
1.可用内存不断减少问题现象
执行free -m
查询内存状态
发现内存越来越少了
2.排查方案:用hcache检查buffer/cache 过高如何排查是由那几个进程引起的
hcache
安装
sudo wget https://silenceshell-1255345740.cos.ap-shanghai.myqcloud.com/hcache -O /usr/local/bin/hcache ;\
chmod +x /usr/local/bin/hcache
2.1hcache
命令查询最大buffer/cache进程号
2.1.1 全局显示10个最大的被缓存文件
hcache --top 10
全局显示10个最大的被缓存文件
hcache --top 10
+------------------------------------------------------------------+----------------+------------+-----------+---------+
| Name | Size (bytes) | Pages | Cached | Percent |
|------------------------------------------------------------------+----------------+------------+-----------+---------|
| /usr/bin/dockerd | 95731632 | 23372 | 16990 | 072.694 |
| /usr/bin/containerd | 52008056 | 12698 | 10690 | 084.186 |
| /usr/lib/x86_64-linux-gnu/libLLVM-13.so.1 | 99813728 | 24369 | 9785 | 040.153 |
| /snap/snapd/19457/usr/lib/snapd/snapd | 33291328 | 8128 | 7488 | 092.126 |
| /usr/lib/x86_64-linux-gnu/dri/crocus_dri.so | 25006904 | 6106 | 2986 | 048.903 |
| /usr/lib/x86_64-linux-gnu/libwebkit2gtk-4.0.so.37.63.4 | 73062112 | 17838 | 2641 | 014.805 |
| /usr/lib/x86_64-linux-gnu/libmozjs-91.so.91.10.0 | 12184728 | 2975 | 2634 | 088.538 |
| /usr/bin/containerd-shim-runc-v2 | 9489240 | 2317 | 2306 | 099.525 |
| /var/log/journal/56abef55033544eaab9e41b9caf03250/system.journal | 8388608 | 2048 | 1216 | 059.375 |
| /usr/bin/python3.10 | 5913032 | 1444 | 1175 | 081.371 |
+------------------------------------------------------------------+----------------+------------+-----------+---------+
2.1.2 查询第一个的进程号
lsof /usr/bin/dockerd
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
dockerd 1099 root txt REG 8,18 95731632 17564073 /usr/bin/dockerd
2.1.3 取当前进程号所打开的所有文件信息
2.1.3.1 hcache
hcache -pid 1099
+--------------------------------------------------+----------------+------------+-----------+---------+
| Name | Size (bytes) | Pages | Cached | Percent |
|--------------------------------------------------+----------------+------------+-----------+---------|
| /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 | 240936 | 59 | 59 | 100.000 |
| /var/lib/docker/buildkit/snapshots.db | 16384 | 4 | 4 | 100.000 |
| /usr/lib/x86_64-linux-gnu/libnss_systemd.so.2 | 309600 | 76 | 76 | 100.000 |
| /var/lib/docker/volumes/metadata.db | 65536 | 16 | 11 | 068.750 |
| /usr/lib/x86_64-linux-gnu/libselinux.so.1 | 166280 | 41 | 41 | 100.000 |
| /usr/lib/x86_64-linux-gnu/libdevmapper.so.1.02.1 | 438864 | 108 | 105 | 097.222 |
| /var/lib/docker/buildkit/cache.db | 32768 | 8 | 5 | 062.500 |
| /usr/lib/x86_64-linux-gnu/libc.so.6 | 2216304 | 542 | 532 | 098.155 |
| /usr/lib/x86_64-linux-gnu/libsystemd.so.0.32.0 | 807936 | 198 | 190 | 095.960 |
| /usr/bin/dockerd | 95731632 | 23372 | 16990 | 072.694 |
| /var/lib/docker/buildkit/containerdmeta.db | 16384 | 4 | 4 | 100.000 |
| /usr/lib/x86_64-linux-gnu/libpcre2-8.so.0.10.4 | 613064 | 150 | 85 | 056.667 |
| /usr/lib/x86_64-linux-gnu/libm.so.6 | 940560 | 230 | 187 | 081.304 |
| /usr/lib/x86_64-linux-gnu/liblzma.so.5.2.5 | 170456 | 42 | 42 | 100.000 |
| /usr/lib/x86_64-linux-gnu/libcap.so.2.44 | 39024 | 10 | 10 | 100.000 |
| /usr/lib/x86_64-linux-gnu/liblz4.so.1.9.3 | 125152 | 31 | 31 | 100.000 |
| /var/lib/docker/buildkit/metadata_v2.db | 16384 | 4 | 4 | 100.000 |
| /usr/lib/x86_64-linux-gnu/libgpg-error.so.0.32.1 | 149760 | 37 | 37 | 100.000 |
| /usr/lib/x86_64-linux-gnu/libgcrypt.so.20.3.4 | 1296312 | 317 | 114 | 035.962 |
| /usr/lib/x86_64-linux-gnu/libzstd.so.1.4.8 | 841808 | 206 | 163 | 079.126 |
| /usr/lib/x86_64-linux-gnu/libudev.so.1.7.2 | 166240 | 41 | 41 | 100.000 |
+--------------------------------------------------+----------------+------------+-----------+---------+
2.1.4 lsof
lsof -p 1099
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
dockerd 1099 root cwd DIR 8,18 4096 2 /
dockerd 1099 root rtd DIR 8,18 4096 2 /
dockerd 1099 root txt REG 8,18 95731632 17564073 /usr/bin/dockerd
dockerd 1099 root mem-W REG 8,18 32768 13250873 /var/lib/docker/buildkit/cache.db
dockerd 1099 root mem-W REG 8,18 16384 13250871 /var/lib/docker/buildkit/metadata_v2.db
dockerd 1099 root mem-W REG 8,18 16384 13250870 /var/lib/docker/buildkit/snapshots.db
dockerd 1099 root mem-W REG 8,18 16384 13250869 /var/lib/docker/buildkit/containerdmeta.db
dockerd 1099 root mem REG 8,18 309600 17564128 /usr/lib/x86_64-linux-gnu/libnss_systemd.so.2
dockerd 1099 root mem-W REG 8,18 65536 13250858 /var/lib/docker/volumes/metadata.db
dockerd 1099 root mem REG 8,18 613064 17569291 /usr/lib/x86_64-linux-gnu/libpcre2-8.so.0.10.4
dockerd 1099 root mem REG 8,18 149760 17570515 /usr/lib/x86_64-linux-gnu/libgpg-error.so.0.32.1
dockerd 1099 root mem REG 8,18 940560 17570772 /usr/lib/x86_64-linux-gnu/libm.so.6
dockerd 1099 root mem REG 8,18 166280 17571132 /usr/lib/x86_64-linux-gnu/libselinux.so.1
dockerd 1099 root mem REG 8,18 1296312 17570433 /usr/lib/x86_64-linux-gnu/libgcrypt.so.20.3.4
dockerd 1099 root mem REG 8,18 841808 17571483 /usr/lib/x86_64-linux-gnu/libzstd.so.1.4.8
dockerd 1099 root mem REG 8,18 2216304 17570121 /usr/lib/x86_64-linux-gnu/libc.so.6
dockerd 1099 root mem REG 8,18 166240 17564812 /usr/lib/x86_64-linux-gnu/libudev.so.1.7.2
dockerd 1099 root mem REG 8,18 39024 17564050 /usr/lib/x86_64-linux-gnu/libcap.so.2.44
dockerd 1099 root mem REG 8,18 125152 17570767 /usr/lib/x86_64-linux-gnu/liblz4.so.1.9.3
dockerd 1099 root mem REG 8,18 170456 17570769 /usr/lib/x86_64-linux-gnu/liblzma.so.5.2.5
dockerd 1099 root mem REG 8,18 438864 17570258 /usr/lib/x86_64-linux-gnu/libdevmapper.so.1.02.1
dockerd 1099 root mem REG 8,18 807936 17563757 /usr/lib/x86_64-linux-gnu/libsystemd.so.0.32.0
dockerd 1099 root mem REG 8,18 240936 17569784 /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
dockerd 1099 root 0r CHR 1,3 0t0 5 /dev/null
dockerd 1099 root 1u unix 0xffff8b1548ec0c00 0t0 25224 type=STREAM
dockerd 1099 root 2u unix 0xffff8b1548ec0c00 0t0 25224 type=STREAM
dockerd 1099 root 3u unix 0xffff8b1551325400 0t0 25422 /var/run/docker/metrics.sock type=STREAM
dockerd 1099 root 4u unix 0xffff8b1552019400 0t0 19442 /run/docker.sock type=STREAM
dockerd 1099 root 5u a_inode 0,14 0 1049 [eventpoll]
dockerd 1099 root 6r FIFO 0,13 0t0 28206 pipe
dockerd 1099 root 7w FIFO 0,13 0t0 28206 pipe
dockerd 1099 root 8u unix 0xffff8b1543359800 0t0 25423 type=STREAM
dockerd 1099 root 9u unix 0xffff8b1547435c00 0t0 28373 type=STREAM
dockerd 1099 root 10uW REG 8,18 65536 13250858 /var/lib/docker/volumes/metadata.db
dockerd 1099 root 11r REG 0,4 0 4026531840 net
dockerd 1099 root 12u netlink 0t0 28473 ROUTE
dockerd 1099 root 13u netlink 0t0 28474 XFRM
dockerd 1099 root 14u netlink 0t0 28475 NETFILTER
dockerd 1099 root 15u unix 0xffff8b1547434800 0t0 28605 /var/run/docker/libnetwork/fe50f8fc06f9.sock type=STREAM
dockerd 1099 root 16u FIFO 0,24 0t0 1610 /run/docker/containerd/32d852ca2c732cc2017657530166bedb0d0e34eba9b7cd2eaa82484e4aef7b1d/init-stdout
dockerd 1099 root 17r FIFO 0,24 0t0 1610 /run/docker/containerd/32d852ca2c732cc2017657530166bedb0d0e34eba9b7cd2eaa82484e4aef7b1d/init-stdout
dockerd 1099 root 18u FIFO 0,24 0t0 1611 /run/docker/containerd/32d852ca2c732cc2017657530166bedb0d0e34eba9b7cd2eaa82484e4aef7b1d/init-stderr
dockerd 1099 root 19r FIFO 0,24 0t0 1611 /run/docker/containerd/32d852ca2c732cc2017657530166bedb0d0e34eba9b7cd2eaa82484e4aef7b1d/init-stderr
dockerd 1099 root 20w REG 8,18 18767 13762653 /var/lib/docker/containers/32d852ca2c732cc2017657530166bedb0d0e34eba9b7cd2eaa82484e4aef7b1d/32d852ca2c732cc2017657530166bedb0d0e34eba9b7cd2eaa82484e4aef7b1d-json.log
dockerd 1099 root 21u FIFO 0,24 0t0 1615 /run/docker/containerd/c05b580cb355531b61b498476e6b5f61816fc5cfbf2fd74a174452f373fce8d7/init-stdout
dockerd 1099 root 22u FIFO 0,24 0t0 1616 /run/docker/containerd/c05b580cb355531b61b498476e6b5f61816fc5cfbf2fd74a174452f373fce8d7/init-stderr
dockerd 1099 root 23r FIFO 0,24 0t0 1615 /run/docker/containerd/c05b580cb355531b61b498476e6b5f61816fc5cfbf2fd74a174452f373fce8d7/init-stdout
dockerd 1099 root 24r FIFO 0,24 0t0 1616 /run/docker/containerd/c05b580cb355531b61b498476e6b5f61816fc5cfbf2fd74a174452f373fce8d7/init-stderr
dockerd 1099 root 25w REG 8,18 762036 13500701 /var/lib/docker/containers/c05b580cb355531b61b498476e6b5f61816fc5cfbf2fd74a174452f373fce8d7/c05b580cb355531b61b498476e6b5f61816fc5cfbf2fd74a174452f373fce8d7-json.log
dockerd 1099 root 26uW REG 8,18 16384 13250869 /var/lib/docker/buildkit/containerdmeta.db
dockerd 1099 root 27uW REG 8,18 16384 13250870 /var/lib/docker/buildkit/snapshots.db
dockerd 1099 root 28uW REG 8,18 16384 13250871 /var/lib/docker/buildkit/metadata_v2.db
dockerd 1099 root 29u sock 0,8 0t0 29001 protocol: NETLINK
dockerd 1099 root 30u sock 0,8 0t0 29829 protocol: UDP
dockerd 1099 root 31u sock 0,8 0t0 29003 protocol: UDP
dockerd 1099 root 32u sock 0,8 0t0 31818 protocol: NETLINK
dockerd 1099 root 33u sock 0,8 0t0 29004 protocol: TCP
dockerd 1099 root 34u sock 0,8 0t0 29830 protocol: TCP
dockerd 1099 root 35uW REG 8,18 32768 13250873 /var/lib/docker/buildkit/cache.db
dockerd 1099 root 38u FIFO 0,24 0t0 1788 /run/docker/containerd/f3ac3845bcaea0baf810c4ffa89097d34f8dc851c949993c90719dbf2df8cdda/init-stdin
dockerd 1099 root 39u FIFO 0,24 0t0 1789 /run/docker/containerd/f3ac3845bcaea0baf810c4ffa89097d34f8dc851c949993c90719dbf2df8cdda/init-stdout
dockerd 1099 root 40w FIFO 0,24 0t0 1788 /run/docker/containerd/f3ac3845bcaea0baf810c4ffa89097d34f8dc851c949993c90719dbf2df8cdda/init-stdin
dockerd 1099 root 41w REG 8,18 149 13788026 /var/lib/docker/containers/f3ac3845bcaea0baf810c4ffa89097d34f8dc851c949993c90719dbf2df8cdda/f3ac3845bcaea0baf810c4ffa89097d34f8dc851c949993c90719dbf2df8cdda-json.log
dockerd 1099 root 42r FIFO 0,24 0t0 1789 /run/docker/containerd/f3ac3845bcaea0baf810c4ffa89097d34f8dc851c949993c90719dbf2df8cdda/init-stdout
dockerd 1099 root 46u sock 0,8 0t0 35021 protocol: NETLINK
2.3 解决方案
除了在系统进程内存使用较大压力的情况下进行内存的回收外,我们也可以进行手动的·buffer/cache·回收,但由于·buffer/cache·主要是用于文件的读写使用,所以进行文件回收时,一般常伴随系统的IO飙高,因为系统内核也对比cache中的数据与硬盘中的数据是否一致,如果不一致需要写入,然后才能进行内存的回收;
$ sync
# 将内存中数据强制先刷新到磁盘中
清理Buffer缓存区域
$ echo 1 > /proc/sys/vm/drop_caches # 表示清除pagecache。
$ echo 2 > /proc/sys/vm/drop_caches # 表示清除回收slab分配器中的对象(包括目录项缓存和inode缓存)。slab分配器是内核中管理内存的一种机制,其中很多缓存数据实现都是用的pagecache。
$ echo 3 > /proc/sys/vm/drop_caches # 表示清除pagecache和slab分配器中的缓存对象
如果你必须清除磁盘高速缓存,第一个命令在企业和生产环境中是最安全,"...echo 1> ..."
只会清除页面缓存。 在生产环境中不建议使用上面的第三个选项"...echo 3 > ..."
,除非你明确自己在做什么,因为它会清除缓存页
,目录项
和inodes
。
创建一个 shell 脚本,通过一个 cron 调度任务在每天下午2点自动清除RAM缓存。如下创建一个 shell 脚本 clearcache.sh
并在其中添加以下行:
#!/bin/bash
# 注意,我们这里使用了 "echo 3",但是不推荐使用在产品环境中,应该使用 echo 1
echo 3 > /proc/sys/vm/drop_caches"
给clearcache.sh
文件设置执行权限
chmod 755 clearcache.sh
现在设置一个每天下午2点的定时任务来清除RAM缓存,打开crontab进行编辑。文章来源:https://www.toymoban.com/news/detail-629382.html
crontab -e
添加以下行,保存并退出。文章来源地址https://www.toymoban.com/news/detail-629382.html
0 3 * * * /root/shell_script/clear_cache.sh
参考文档:
- https://blog.csdn.net/imliuqun123/article/details/130149771
- https://huaweicloud.csdn.net/63566961d3efff3090b5e461.html?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Eactivity-1-125906007-blog-130149771.235%5Ev38%5Epc_relevant_anti_vip&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Eactivity-1-125906007-blog-130149771.235%5Ev38%5Epc_relevant_anti_vip&utm_relevant_index=2
到了这里,关于【linux】关于内存free转换到buffer/cache之后,内存被用完的解决思路的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!