清理linux内存cache

在使用grep从很多文件中搜索特定数据串的时候,发现内存使用迅速提高,主要是cache的使用占用了相当多的内存。在使用下面命令的时候忽视了文件的数量和文件大小,导致cache突增。
 
  1. # grep -e "dst_string" ./*
     大家在使用shell编程的时候一定要注意通配符的使用,这里尤其提醒大家就是星号(*)的使用,星号固然方便,但要适度使用。
 
      这里主要还是记录如何手动清理linux内存cache,因为上面的操作使用的大量的cache。
 
1、使用free查看一下当前内存使用情况:
 
  1. # free
  2.              total used free shared buffers cached
  3. Mem: 16621896 8967952 7653944 0 212352 8377276
  4. -/+ buffers/cache: 378324 16243572
  5. Swap: 10241428 0 10241428
2、执行sync同步数据
 
  1. # sync
      该命令将未写的系统缓冲区写到磁盘中。包含已修改的 i-node、已延迟的块 I/O 和读写映射文件。
 
3、清理cache
 
  1. # echo 3 > /proc/sys/vm/drop_caches
  2. # free
  3.              total used free shared buffers cached
  4. Mem: 16621896 579592 16042304 0 268 308708
  5. -/+ buffers/cache: 270616 16351280
  6. Swap: 10241428 0 10241428
4、对比一下加粗部分就清楚了,cache被清理掉了。
 
参考资料:
 
5、drop_cache的详细文档如下,以便查阅(摘自: http://www.linuxidc.com/Linux/2010-03/24939.htm
 
  1. Writing to this will cause the kernel to drop clean caches, dentries and inodes from memory, causing that memory to become free.
  2. To free pagecache:
  3. * echo 1 > /proc/sys/vm/drop_caches
  4. To free dentries and inodes:
  5. * echo 2 > /proc/sys/vm/drop_caches
  6. To free pagecache, dentries and inodes:
  7. * echo 3 > /proc/sys/vm/drop_caches
  8. 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.
  9. This tunable was added in 2.6.16.


原文地址:http://blog.chinaunix.net/uid-25505925-id-180921.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值