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