Linux手动清理Cache

软件版本:
  操作系统:ubuntu10.04
    内核版本:Linux version 2.6.32-36-generic

本文内容:

  假如某程序频繁的存取大量文件将会导致 cache 的使用量剧增,并且不会在该程序退出后自动释放。如果大量内存一直被 cache 占用,系统会变得非常慢,甚至连敲命令都卡。我们可以通过对 /proc/sys/vm/drop_caches 这个文件写入不同的数值以达到清理 cache 的目的。

  首先要运行 sync 将为写入的缓冲回写到磁盘中。

  $ sync

  将后往 /proc/sys/vm/drop_caches 写入数值 3 。

  $ echo 3 > /proc/sys/vm/drop_caches

  关于 drop_caches 的详细说明:

  Writing to this will cause the kernel to drop clean caches, dentries and inodes from memory, causing that memory to become free.
  To free pagecache:
  * echo 1 > /proc/sys/vm/drop_caches
  To free dentries and inodes:
  * echo 2 > /proc/sys/vm/drop_caches
  To free pagecache, dentries and inodes:
  * echo 3 > /proc/sys/vm/drop_caches
  As this is a non-destructive operation, and dirty objects are notfreeable, the user should run "sync" first in order to make sure allcached objects are freed.
  This tunable was added in 2.6.16.

posted @ 2012-05-14 17:11  Eddy_He  阅读(595)  评论(0编辑  收藏  举报