I wrote a little script to monitor memory usage and send an e-mail when free cached memory drops below 1000 MB, but is greater than 800 MB:
#!/bin/bash
while [ true ] ;do
used=`free -m |awk 'NR==3 {print $4}'`
if [ $used -lt 1000 ] && [ $used -gt 800 ]; then
echo "Free memory is below 1000MB. Possible memory leak!!!" | /bin/mail -s "HIGH MEMORY ALERT!!!" user@mydomain.com
sleep 5
fi
done
The script works very well but, for some reason, it has a 10% constant CPU usage (and we are talking about a new generation quad-core Xeon processor. I have much bigger scripts that stay well below 1% CPU usage.
Any idea what's causing this and how I can optimize it?