问题描述
Linux内存使用量超过阈值,使得应用程序无可用内存,最终导致程序崩溃。即使在程序没有挂掉时把程序停掉,系统内存也不会被释放。
手动化解决方案:
三条指令:
sync
echo 1 > /proc/sys/vm/drop_caches
echo 2 > /proc/sys/vm/drop_caches
echo 3 > /proc/sys/vm/drop_caches
自动化执行方案:
#! /bin/sh used=`free -m | awk 'NR==2' | awk '{print $3}'` free=`free -m | awk 'NR==2' | awk '{print $4}'` echo "===========================" >> /app/memory/logs/mem.log date >> /app/memory/logs/mem.log echo "Memory usage before | [Use:${used}MB][Free:${free}MB]" >> /app/memory/logs/mem.log if [ $free -le 4000 ] ; 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 used_ok=`free -m | awk 'NR==2' | awk '{print $3}'` free_ok=`free -m | awk 'NR==2' | awk '{print $4}'` echo "Memory usage after | [Use:${used_ok}MB][Free:${free_ok}MB]" >> /app/memory/logs/mem.log echo "OK" >> /app/memory/logs/mem.log else echo "Not required" >> /app/memory/logs/mem.log fi exit 1
使用crontab -e命令编辑当前用户的crontab
0 6 * * * /usr/local/tomcat/sztFileFront/bin/freemem.sh