50

如何查看 Linux 当前缓存了哪些大文件?

 5 years ago
source link: https://www.linuxprobe.com/cache-in-linux.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

一个可以查看 Linux 当前缓存了哪些大文件的小工具

Linux 用户可能经常遇到的一个问题是,机器有 16GB 内存之多,运行的进程也不多,但是剩下的 free 内存并不多,大部分都被 buff 和 cache 占用了(比如下面我的 PC)。

$ free -h
              total        used        free      shared  buff/cache   available
Mem:            15G        6.4G        2.3G        1.1G        6.9G        7.7G
Swap:          975M        268K        975M

虽然对于 Linux 的内存分配机制来说,这种被 buff 和 cache 占用的内存在系统需要时会及时释放出来,但是有时候我们想知道到底 cache 了些什么却没有一个好的办法。

前几天看到有个人用 go 写了个 pcstat,可以查看某个文件是否被缓存(作者的目的是数据库调优),也可以根据进程的 PID 来查看都缓存了哪些文件,不过该工具不能查看整个操作系统都 cache 了哪些文件。

因此,我基于 pcstat 写了个hcache,增加了查看当前操作系统所 cache 的前若干个文件的特性。如果有什么建议的话也欢迎反馈给我:[email protected]

使用方法比较简单:

$ sudo hcache --top 10
[sudo] password for silenceshell:
+----------------------------------------------------------------------------------+----------------+------------+-----------+---------+
| Name                                                                             | Size (bytes)   | Pages      | Cached    | Percent |
|----------------------------------------------------------------------------------+----------------+------------+-----------+---------|
| /opt/google/chrome/chrome                                                        | 114911208      | 28055      | 25457     | 090.740 |
| /usr/share/code/code                                                             | 67688720       | 16526      | 12274     | 074.271 |
| /home/silenceshell/Software/pycharm-community-2016.2/lib/pycharm.jar                   | 95177431       | 23237      | 11325     | 048.737 |
| /opt/atom/atom                                                                   | 62641344       | 15294      | 10578     | 069.164 |
| /usr/bin/dockerd                                                                 | 39121168       | 9552       | 7103      | 074.361 |
| /home/silenceshell/Software/pycharm-community-2016.2/jre/jre/lib/amd64/libjfxwebkit.so | 57455824       | 14028      | 6625      | 047.227 |
| /usr/lib/x86_64-linux-gnu/libQtWebKit.so.4.10.2                                  | 36462184       | 8902       | 6316      | 070.950 |
| /usr/lib/beyondcompare/BCompare                                                  | 30640160       | 7481       | 5505      | 073.586 |
| /usr/bin/SecureCRT                                                               | 29524560       | 7209       | 4806      | 066.667 |
| /usr/share/code/libnode.so                                                       | 21135976       | 5161       | 4588      | 088.898 |
+---------------------------------------------------------------------

让我们来查看一下其中列出的一个文件:

$ lsof /usr/lib/x86_64-linux-gnu/libQtWebKit.so.4.10.2
COMMAND    PID   USER  FD   TYPE DEVICE SIZE/OFF    NODE NAME
quiterss 20630 silenceshell mem    REG    8,5 36462184 3936610 /usr/lib/x86_64-linux-gnu/libQtWebKit.so.4.10.2

默认情况下会显示 cache 的文件的全路径,会比较长,也可以使用--bname选项来仅显示文件名:

-------------+----------------+------------+-----------+---------+
$ sudo ./hcache --top 3  --bname  
+-------------+----------------+------------+-----------+---------+
| Name        | Size (bytes)   | Pages      | Cached    | Percent |
|-------------+----------------+------------+-----------+---------|
| chrome      | 114911208      | 28055      | 25476     | 090.807 |
| pycharm.jar | 95177431       | 23237      | 11479     | 049.400 |
| atom        | 62641344       | 15294      | 10578     | 069.164 |
+-------------+----------------+------------+-----------+---------+

这个小工具的源码开源在 Github上:hcache,欢迎关注和提交 issue 及 PR。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK