/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:           1825         381        1363           8          79        1413
Swap:             0           0           0
# cp /etc/* /mytest/
# free -m
              total        used        free      shared  buff/cache   available
Mem:           1825         382        1341           8         101        1412
Swap:             0           0           0

过一段时间:

# free -m
              total        used        free      shared  buff/cache   available
Mem:           1825         382        1341           8         101        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          37        1413
Swap:  

3. 写个脚本做这个事情

# cat cleanup_cache.sh 
#!/bin/sh
# drop_caches for every 5 mins

drop_caches() {
    echo "Drop caches."
    sync
    echo 3 > /proc/sys/vm/drop_caches &
    return 0
}

while true; do
    sleep 300
    drop_caches
done

exit 0

4. 结论

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

猜你喜欢

转载自www.cnblogs.com/hellokitty2/p/10088042.html
今日推荐