hcache查看linux系统缓存的占用情况
hcache是基于pcstat的,pcstat可以查看某个文件是否被缓存和根据进程pid来查看都缓存了哪些文件。hcache在其基础上增加了查看整个操作系统Cache和根据使用Cache大小排序的特性。
查看到缓存的文件后,可以确认是否是自己应用的文件,如果是应用导致的,请优化代码,或者通过crontab定时清理缓存或者将vm.extra_free_kbytes设置为vm.min_free_kbytes和一样大,提高low阈值,触发操作系统自动回收缓存。https://www.cnblogs.com/panfeng412/p/drop-caches-under-linux-system-2.html
安装
# wget https://silenceshell-1255345740.cos.ap-shanghai.myqcloud.com/hcache # chmod 755 hcache ;mv hcache /usr/local/bin/
使用
1、查看占用缓存最多的三个文件
# hcache -top 3 2021/06/03 21:01:28 skipping "/tmp/hsperfdata_test/531": could not open file for read: open /tmp/hsperfdata_test/531: no such file or directory +------------------------------------------------------------------+----------------+------------+-----------+---------+ | Name | Size (bytes) | Pages | Cached | Percent | |------------------------------------------------------------------+----------------+------------+-----------+---------| | /home/test/softwares/jdk1.8.0_191/jre/lib/rt.jar | 66346586 | 16198 | 4559 | 028.145 | | /home/test/softwares/jdk1.8.0_191/jre/lib/amd64/server/libjvm.so | 17022752 | 4156 | 3734 | 089.846 | | /etc/udev/hwdb.bin | 7780559 | 1900 | 958 | 050.421 | +------------------------------------------------------------------+----------------+------------+-----------+---------+
结果展示含义如下:
- name:占用缓存的文件全路径
- size:文件大小
- pages:占了多少个Block
- Cached:缓存使用了多少个Block
- Percent:Cached 与 Pages的比值
上面命令显示的是文件全路径,可以使用-bname选项,只显示文件名
# hcache -top 3 -bname 2021/06/03 21:11:44 skipping "/tmp/hsperfdata_test/531": could not open file for read: open /tmp/hsperfdata_test/531: no such file or directory +-----------+----------------+------------+-----------+---------+ | Name | Size (bytes) | Pages | Cached | Percent | |-----------+----------------+------------+-----------+---------| | rt.jar | 66346586 | 16198 | 4559 | 028.145 | | libjvm.so | 17022752 | 4156 | 3734 | 089.846 | | hwdb.bin | 7780559 | 1900 | 958 | 050.421 | +-----------+----------------+------------+-----------+---------+
查看进程的缓存使用
# hcache -pid 1397 +-----------------------+----------------+------------+-----------+---------+ | Name | Size (bytes) | Pages | Cached | Percent | |-----------------------+----------------+------------+-----------+---------| | libm-2.23.so | 1088952 | 266 | 185 | 069.549 | | libstdc++.so.6.0.21 | 1566440 | 383 | 346 | 090.339 | | libz.so.1.2.8 | 104824 | 26 | 26 | 100.000 | | libdl-2.23.so | 14608 | 4 | 4 | 100.000 | | libwrap.so.0.7.6 | 36632 | 9 | 9 | 100.000 | | libaio.so.1.0.1 | 5512 | 2 | 2 | 100.000 | | libnss_compat-2.23.so | 35688 | 9 | 9 | 100.000 | | libnsl-2.23.so | 93128 | 23 | 23 | 100.000 | | libc-2.23.so | 1864888 | 456 | 456 | 100.000 | | libcrypt-2.23.so | 39224 | 10 | 10 | 100.000 | | librt-2.23.so | 31712 | 8 | 8 | 100.000 | | liblz4.so.1.7.1 | 96360 | 24 | 24 | 100.000 | | libgcc_s.so.1 | 89696 | 22 | 22 | 100.000 | | libpthread-2.23.so | 138696 | 34 | 34 | 100.000 | | libnss_nis-2.23.so | 47648 | 12 | 12 | 100.000 | | libnuma.so.1.0.0 | 43936 | 11 | 11 | 100.000 | | ld-2.23.so | 162632 | 40 | 40 | 100.000 | | mysqld | 24754056 | 6044 | 4051 | 067.025 | | libnss_files-2.23.so | 47600 | 12 | 12 | 100.000 | +-----------------------+----------------+------------+-----------+---------+