Linux下清除缓存 drop_caches,sysctl(备忘)

1. 用命令 free -m 查看一下内存的使用情况:

然后清除缓存后再来查看一下内存的使用情况:

前后对比可发现,缓存由之前的110M缩小到了25M,效果比较明显。

2. 下面说一下 drop_caches:
清空 pagecache:
sync
echo 1 > /proc/sys/vm/drop_caches
或者:
sync
sysctl -w vm.drop_caches=1
清空 dentries 和 inodes:
sync
echo 2 > /proc/sys/vm/drop_caches
或者:
sync
sysctl -w vm.drop_caches=2


清空所有缓存(pagecache、dentries 和 inodes):
sync
echo 3 > /proc/sys/vm/drop_caches
或者:
sync
sysctl -w vm.drop_caches=3


3. 如果尝试向 drop_caches 中写入 0,会报下面错误:
-bash: echo: write error: Invalid argument
我的内核是3.4.39,64 位。至于什么原因,我没找到。但是从下面关于 drop_caches 的说明来看,drop_caches 的值并没有说可以设置成 0 的,但是 drop_caches 的默认值是 0。但是 2.6.x 的内核将 drop_caches 的值设置为 0 是没有问题的。


关于 drop_caches:
Kernels 2.6.16 and newer provide a mechanism to have the kernel drop the page cache and/or inode and dentry caches on command, which can help free up a lot of memory. Now you can throw away that script that allocated a ton of memory just to get rid of the cache...


To use /proc/sys/vm/drop_caches, just echo a number to it.

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


This is a non-destructive operation and will only free things that are completely unused. Dirty objects will continue to be in use until written out to disk and are not freeable. If you run "sync" first to flush them out to disk, these drop operations will tend to free more memory.

参考:

http://linux-mm.org/Drop_Caches

https://bbs.archlinux.org/viewtopic.php?id=127049
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值