/proc/sys/vm/drop_caches 清理缓存

时间:2023-03-09 01:33:54
/proc/sys/vm/drop_caches 清理缓存

1. 使用方法

/proc/sys/vm/drop_caches默认是0

# echo 1 > /proc/sys/vm/drop_caches; free pagecache, use
# echo 2 > /proc/sys/vm/drop_caches; free dentries and inodes
# echo 3 > /proc/sys/vm/drop_caches; free pagecache, dentries and inodes

注意:使用之前需要先sync,将缓存刷到磁盘中。

2. 实验

单位:MB

# free -m
total used free shared buff/cache available
Mem: 79
Swap:
# cp /etc/* /mytest/
# free -m
total used free shared buff/cache available
Mem: 1825 382 1341 8 1412
Swap: 0 0 0 过一段时间: # free -m
total used free shared buff/cache available
Mem: 1825 382 1341 8 1412
Swap: 0 # cat /proc/sys/vm/drop_caches
0 # sync # echo 3 > /proc/sys/vm/drop_caches
# free -m
total used free shared buff/cache available
Mem: 1825 382 1405 8 1413
Swap:

3. 写个脚本做这个事情

# cat cleanup_cache.sh
#!/bin/sh
# drop_caches for every mins drop_caches() {
echo "Drop caches."
sync
echo > /proc/sys/vm/drop_caches &
return
} while true; do
sleep
drop_caches
done exit

4. 结论

Linux内核默认保持drop_cache的值是0,不建议经常修改它。