在Linux下频繁存取文件后,物理内存会很快被用光,当程序结束后,内存不会被正常释放,而是一直作为caching. 这样通过free 查看系统内存, 就会发现物理内存剩余很少。
# cat freemem.sh
###############################################################
#!/bin/bash
used=`free -m | awk 'NR==2' | awk '{print $3}'`
free=`free -m | awk 'NR==2' | awk '{print $4}'`
echo "===========================" >> /var/log/mem.log
date >> /var/log/mem.log
echo "Memory usage | [Use:${used}MB][Free:${free}MB]" >> /var/log/mem.log
if [ $free -le 100 ]; then
sync && echo 1 > /proc/sys/vm/drop_caches
sync && echo 2 > /proc/sys/vm/drop_caches
sync && echo 3 > /proc/sys/vm/drop_caches
echo "OK" >> /var/log/mem.log
else
echo "Not required" >> /var/log/mem.log
fi
###############################################################
参考
深度清理系统内存命令 freemem
https://zongf.blog.csdn.net/article/details/90444905
Linux服务器释放内存脚本
https://blog.csdn.net/xiaolong_4_2/article/details/82740446
scripts/freemem.sh
https://github.com/cbodden/scripts/blob/master/freemem.sh
这样释放内存好吗? sync; echo 3 > /proc/sys/vm/drop_caches 会不会影响其他正在跑的程序?
https://www.v2ex.com/t/160359
https://www.kernel.org/doc/Documentation/sysctl/vm.txt