[Linux] Linux下谁在消耗我们的cache

一、缘由:

  曾经看到MySQL服务器上Cache占用特别大,其实大家都知道这是合理的,这些都是可用内存;

那么问题来了,是谁在占用这些Cache呢?如果去掉不合理的Cache占用,系统内存会更充分的得到利用。

             total       used       free     shared    buffers     cached
Mem:         64309      55889       8420          0        240      43363
-/+ buffers/cache:      12285      52024
Swap:        20479          0      20479

  由上图可见,Cache占用了42G内存,猛地一看很可怕。实际上他还属于memfree之列。具体说明,可以看这里:

http://www.linuxatemyram.com/

 

二、解决办法:

  这里有两个工具可以查看某个文件是否占用了Page Cache,占用了多少:fincore和vmtouch

1、fincore 

  这是linux-ftools工具的一部分,你给出文件(夹)名作为输入,他会告诉你他有多少文件(数据)被系统缓存起来。

因为不太会安装,这里不做推荐,具体看这里https://code.google.com/p/linux-ftools/

 

2、vmtouch

  vmtouch可以查到缓存的文件和目录、把文件推入缓存和驱逐缓存中的文件等等。(推荐

 

安装方法:

 

$ git clone https://github.com/hoytech/vmtouch
$ cd vmtouch
$ make
$ sudo make install

 

 

 

使用方法:

$ vmtouch
vmtouch: no files or directories specified

vmtouch v1.0.2 - the Virtual Memory Toucher by Doug Hoyte
Portable file system cache diagnostics and control

Usage: vmtouch [OPTIONS] ... FILES OR DIRECTORIES ...

Options:
  -t touch pages into memory
  -e evict pages from memory
  -l lock pages in physical memory with mlock(2)
  -L lock pages in physical memory with mlockall(2)
  -d daemon mode
  -m max file size to touch
  -p use the specified portion instead of the entire file
  -f follow symbolic links
  -h also count hardlinked copies
  -w wait until all pages are locked (only useful together with -d)
  -v verbose
  -q quiet

 

 

例子:

1)查看/tmp目录在内存中的缓存:

$ vmtouch /tmp/
vmtouch: WARNING: skipping non-regular file: /tmp/ssh-GgJnCEkWMQC2/agent.1068

           Files: 17
     Directories: 7
  Resident Pages: 4780/4780  18M/18M  100%
         Elapsed: 0.001006 seconds

 

 

详细信息查看可使用-v参数  例如:vmtouch -v /tmp/

 

2)查看一个文件被缓存了多少:

$ vmtouch -v ~/Downloads/phoronix-test-suite_6.0.1_all.deb 
/home/neo/Downloads/phoronix-test-suite_6.0.1_all.deb
[                                            ] 0/132

           Files: 1
     Directories: 0
  Resident Pages: 0/132  0/528K  0%
         Elapsed: 0.000117 seconds

 

 

3)把文件缓存起来:

$ vmtouch -vt ~/Downloads/phoronix-test-suite_6.0.1_all.deb 
/home/neo/Downloads/phoronix-test-suite_6.0.1_all.deb
[OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO] 132/132

           Files: 1
     Directories: 0
   Touched Pages: 132 (528K)
         Elapsed: 0.007935 seconds

 

 

4)把缓存中的数据驱逐出去:

$ vmtouch -ve ~/Downloads/phoronix-test-suite_6.0.1_all.deb 
Evicting /home/neo/Downloads/phoronix-test-suite_6.0.1_all.deb

           Files: 1
     Directories: 0
   Evicted Pages: 132 (528K)
         Elapsed: 0.000109 seconds

 

 

附:

具体信息参考官网:https://hoytech.com/vmtouch/

 

另一个参考页面:vmtouch: portable file cache analyzer

 

 

 

 

 

  

 

posted @ 2016-10-12 15:56  Devops达人  阅读(2631)  评论(0编辑  收藏  举报