#! /bin/bash
# 需要释放内存的,内存使用百分比,可以传参,默认是85%
max_rate=$1
if [ ! "$max_rate" ] ; thenmax_rate=85
fi
echo "max_rate: $max_rate"total=`free -m | awk 'NR==2' | awk '{print $2}'`
used=`free -m | awk 'NR==2' | awk '{print $3}'`
free=`free -m | awk 'NR==2' | awk '{print $4}'`
rate=$(($used*100/$total));
log=/app/mem.log
echo "===========================" >> $log
date >> $log
echo "current_rate: $rate"
echo "Memory usage | [Total:${total}MB][Use:${used}MB][Free:${free}MB]" >> $log
if [ "$rate" -ge "$max_rate" ] ; thensync && echo 1 > /proc/sys/vm/drop_cachessync && echo 2 > /proc/sys/vm/drop_cachessync && echo 3 > /proc/sys/vm/drop_cachesecho "OK" >> $log
elseecho "Not required" >> $log
fi