专业的编程技术博客社区

网站首页 > 博客文章 正文

使用hcache排序当前内存中的缓存文件,找出内存缓存大户

baijin 2024-08-10 13:36:34 博客文章 14 ℃ 0 评论


在内存告警中,经常会遇到系统OOM,登陆服务器查看大部分内存是被cache 使用了。

通过cat /proc/meminfo 或者free命令我也只能看出内存大部分是被buff/cache,至于是哪些程序占用了,毫无头绪。所以需要一个工具能帮助我去查看下当前系统环境下内存缓存的使用情况,最好能做个排序,从而一目了然知道缓存文件大户。

答案是 hcache - a tool fork from pcstat, with a feature that showing top X biggest cache files globally,来源于pcstat,新增了top功能对缓存文件大小进行排序。

https://github.com/silenceshell/hcache

构建

需要go版本> 1.12

git clone https://github.com/silenceshell/hcache.git
cd hcache
make build
sudo cp hcache /usr/local/bin/ 

或者直接下载hcache作者已构建好的,下载地址如下:

https://silenceshell-1255345740.cos.ap-shanghai.myqcloud.com/hcache

hcache的命令介绍

-bname 路径名称转换成简写名称

-histo 直方图显示缓存文件比例

-json 返回json数据

-pid int 返回特定pid的数据

-terse 显示简短输出

示例

./hcache -top 10

可以看到是按照cache的大小从大到小排序,从而很直观看到那些文件路径占用了缓存页面。

再结合lsof就可以找到文件的进程信息了。

./hcache -top 10 -bname

+--------------------------+----------------+------------+-----------+---------+
| Name                             | Size (bytes)    | Pages       | Cached    | Percent |
|--------------------------+----------------+------------+-----------+---------|
| containerd                     | 49085976       | 11984      | 8420         | 070.260 |
| dockerd                         | 102122240     | 24933      | 1786         | 007.163 |
| system.journal               | 24834048       | 6063        | 1106         | 018.242 |
| libmysqlclient.so.18.0.0 | 3135664         | 766          | 766           | 100.000 |
| containerd-shim            | 6120256         | 1495        | 621           | 041.538 |
| hcache                           | 2542252         | 621          | 621           | 100.000 |
| libcrypto.so.1.0.2k         | 2521144         | 616          | 616           | 100.000 |
| libc-2.17.so                    | 2156240         | 527          | 527           | 100.000 |
| libdb-5.3.so                    | 1850600        | 452          | 452           | 100.000 |
| libpython2.7.so.1.0        | 1847752         | 452          | 452           | 100.000 |
+--------------------------+----------------+------------+-----------+---------+

hcache -pid 1

./hcache -top 10 -histo

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表