<span class="z textcut" style="text-align: left; color: rgb(153, 153, 153); text-transform: none; text-indent: 0px; letter-spacing: normal; overflow: hidden; font-family: &quot;Lantinghei SC&quot;, &quot;Helvetica Neue&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, Arial, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; font-size: 12px; font-style: normal; font-weight: normal; word-spacing: 0px; float: left; white-space: nowrap; -ms-word-wrap: break-word; -ms-text-overflow: ellipsis; max-width: 60%; orphans: 2; widows: 2; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">转载自: [http://www.datastart.cn/tech/2017/05/20/hcache.html](http://www.datastart.cn/tech/2017/05/20/hcache.html)</span><span class="y" style="text-align: left; color: rgb(153, 153, 153); text-transform: none; text-indent: 0px; letter-spacing: normal; padding-left: 5px; font-family: &quot;Lantinghei SC&quot;, &quot;Helvetica Neue&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, Arial, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; font-size: 12px; font-style: normal; font-weight: normal; word-spacing: 0px; float: right; white-space: normal; -ms-word-wrap: break-word; orphans: 2; widows: 2; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">作者: 伊布</span>

&nbsp;

linux用户可能经常遇到的一个问题是,机器有16GB内存,开的进程也不多,但是剩下的free内存并不多,大部分都被buff/cache掉了(比如下面我的PC),但是cache了些什么东西并不清楚,linux也没有个命令能查看。


$ <span class="hljs-built_in" style="color: rgb(245, 135, 31); box-sizing: inherit;">free</span> -h total used <span class="hljs-built_in" style="color: rgb(245, 135, 31); box-sizing: inherit;">free</span> shared buff/cache available Mem: <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">15</span>G <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">6.4</span>G <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">2.3</span>G <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">1.1</span>G <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">6.9</span>G <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">7.7</span>G Swap: <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">975</span>M <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">268</span>K <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">975</span>M

前几天看到有个人用go写了个[pcstat](https://github.com/tobert/pcstat),可以查看某个文件是否被缓存(作者的目的是数据库调优),也可以根据进程pid来查看都缓存了哪些文件,但是不能查看整个操作系统都cache了哪些文件。[https://github.com/tobert/pcstat](https://github.com/tobert/pcstat)

因此,我基于pcstat写了个hcache,增加了查看当前操作系统cache的前top个文件的特性,你可以从[这里](http://7xir15.com1.z0.glb.clouddn.com/hcache)下载下来试用

##
Building

`git clone https://github.com/silenceshell/hcache.git
cd {CURRENT_DIR}/hcache
export GOPATH={CURRENT_DIR}/hcache
go get github.com/tobert/pcstat
go build
cp hcache /usr/local/bin
`

##
Requirements

Go 1.4 or higher and golang.org/x/sys/unix.

使用比较简单:


$ sudo hcache --top <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">10</span> [sudo] password <span class="hljs-keyword" style="color: rgb(137, 89, 168); box-sizing: inherit;">for</span> silenceshell: +----------------------------------------------------------------------------------+----------------+------------+-----------+---------+ | Name | Size (bytes) | Pages | Cached | Percent | |----------------------------------------------------------------------------------+----------------+------------+-----------+---------| | <span class="hljs-regexp" style="color: rgb(200, 40, 41); box-sizing: inherit;">/opt/g</span>oogle/chrome/chrome | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">114911208</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">28055</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">25457</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">090.740</span> | | <span class="hljs-regexp" style="color: rgb(200, 40, 41); box-sizing: inherit;">/usr/</span>share/code/code | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">67688720</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">16526</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">12274</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">074.271</span> | | <span class="hljs-regexp" style="color: rgb(200, 40, 41); box-sizing: inherit;">/home/</span>silenceshell/Software/pycharm-community-<span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">2016.2</span>/lib/pycharm.jar | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">95177431</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">23237</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">11325</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">048.737</span> | | <span class="hljs-regexp" style="color: rgb(200, 40, 41); box-sizing: inherit;">/opt/</span>atom/atom | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">62641344</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">15294</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">10578</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">069.164</span> | | <span class="hljs-regexp" style="color: rgb(200, 40, 41); box-sizing: inherit;">/usr/</span>bin/dockerd | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">39121168</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">9552</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">7103</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">074.361</span> | | <span class="hljs-regexp" style="color: rgb(200, 40, 41); box-sizing: inherit;">/home/</span>silenceshell/Software/pycharm-community-<span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">2016.2</span>/jre/jre/lib/amd64/libjfxwebkit.so | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">57455824</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">14028</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">6625</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">047.227</span> | | <span class="hljs-regexp" style="color: rgb(200, 40, 41); box-sizing: inherit;">/usr/</span>lib/x86_64-linux-gnu/libQtWebKit.so.4.10.2 | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">36462184</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">8902</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">6316</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">070.950</span> | | <span class="hljs-regexp" style="color: rgb(200, 40, 41); box-sizing: inherit;">/usr/</span>lib/beyondcompare/BCompare | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">30640160</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">7481</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">5505</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">073.586</span> | | <span class="hljs-regexp" style="color: rgb(200, 40, 41); box-sizing: inherit;">/usr/</span>bin/SecureCRT | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">29524560</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">7209</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">4806</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">066.667</span> | | <span class="hljs-regexp" style="color: rgb(200, 40, 41); box-sizing: inherit;">/usr/</span>share/code/libnode.so | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">21135976</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">5161</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">4588</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">088.898</span> | +----------------------------------------------------------------------------------+----------------+------------+-----------+---------+ $ $ sudo ./hcache --top <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">3</span> --bname +-------------+----------------+------------+-----------+---------+ | Name | Size (bytes) | Pages | Cached | Percent | |-------------+----------------+------------+-----------+---------| | chrome | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">114911208</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">28055</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">25476</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">090.807</span> | | pycharm.jar | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">95177431</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">23237</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">11479</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">049.400</span> | | atom | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">62641344</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">15294</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">10578</span> | <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">069.164</span> | +-------------+----------------+------------+-----------+---------+ $ $ lsof /usr/lib/x86_64-linux-gnu/libQtWebKit.so.4.10.2 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME quiterss <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">20630</span> silenceshell mem REG <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">8</span>,<span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">5</span> <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">36462184</span> <span class="hljs-number" style="color: rgb(245, 135, 31); box-sizing: inherit;">3936610</span> /usr/lib/x86_64-linux-gnu/libQtWebKit.so.4.10.2

源码开源在Github上:[hcache](https://github.com/silenceshell/hcache)

[https://github.com/silenceshell/hcache](https://github.com/silenceshell/hcache)