Activities

May 2010
M T W T F S S
« Apr   Jun »
 12
3456789
10111213141516
17181920212223
24252627282930
31  

Script for checking RAM usage and free up the memory

Hello,

My current situation is one of my dev. server showing “out of memory” issues and sometimes it’s stayed on HUNG state. Only a reboot can help us to make it live. During the server monitoring, I found that ‘httpd”daemon is causing memory outage. So I try to check after the web restart and found that Apache is eating up memory and caching the files on disk. May be it’s a Application leakage or memory leakage.

#List out first 10 memory consuming process

[root@dev scripts]# ps auxf | sort -nr -k 4 | head -10
apache 27206 0.0 12.5 414176 129156 ? S 05:33 0:00 _ /usr/sbin/httpd
apache 27200 0.0 12.5 413152 128652 ? S 05:33 0:00 _ /usr/sbin/httpd
apache 27205 0.0 12.4 412128 127900 ? S 05:33 0:00 _ /usr/sbin/httpd
apache 27198 0.0 12.4 412128 127924 ? S 05:33 0:00 _ /usr/sbin/httpd
mysql 27163 0.0 2.7 255676 28488 ? Sl 05:33 0:00 _ /usr/libexec/mysqld –basedir=/usr –datadir=/var/lib/mysql –user=mysql –pid-file=/var/run/mysqld/mysqld.pid –skip-external-locking –socket=/var/lib/mysql/mysql.sock
apache 27203 0.0 1.4 298640 15384 ? S 05:33 0:00 _ /usr/sbin/httpd
apache 27204 0.0 1.3 296592 13544 ? S 05:33 0:00 _ /usr/sbin/httpd
apache 27728 0.0 1.1 294544 12176 ? S 05:40 0:00 _ /usr/sbin/httpd
apache 27202 0.0 1.1 294544 12196 ? S 05:33 0:00 _ /usr/sbin/httpd
apache 27201 0.0 1.1 294544 12260 ? S 05:33 0:00 _ /usr/sbin/httpd

Here is my sample script which I uses to restart the httpd when the free memory showing less than 100MB in size.

#!/bin/bash
mem_lmt=100 # Size in MB
free_mem=$(free | grep "Mem:" | awk '{print $4}')
free_mem=$((free_mem/1024))
#echo $free_mem
if [ $free_mem -lt $mem_lmt ]
then
/etc/init.d/mysqld restart
/etc/init.d/httpd restart
echo "Apache and MySQL restarted on `date +'%Y-%m-%d %H:%M:%S'`. Available RAM is ${current}%"  >> /var/log/memchecker.log
echo " Apahache eating up RAM caused this service restart" | mail -s " Apache restarted " liju@mydomain.com
#else
#echo "memory status is safe and free memory is " $free_mem "MB"
fi


### Scripts for checking httpd status and restart whether it’s stopped #####
/bin/netstat -ln | /bin/grep “:80 ” | /usr/bin/wc -l | /bin/awk ‘{if ($1 == 0) system(“/sbin/service httpd restart”) }’

Following command will help you to free up the cached memory.

#sync;echo 3 > /proc/sys/vm/drop_caches

2 comments to Script for checking RAM usage and free up the memory

  • Thanks for the script. I noticed that it isn’t taking into account the cached memory. Cached memory while not labeled as “free” is still available in the sense that matters. If you have 0 memory free and 200mb cached, the cache will be freed up as needed to support the system. At least that’s how it’s supposed to work.

    Anyway, I tweaked the script to consider “cached” memory as free. This resets apache now when the free memory + cache is below 100mb.

    #!/bin/bash
    mem_lmt=100 # Size in MB
    free_mem=$(free | grep “Mem:”| awk ‘{print $4}’)
    free_mem=$((free_mem/1024))
    cached_mem=$(free | grep “Mem:”| awk ‘{print $7}’)
    cached_mem=$((cached_mem/1024))
    free_mem=$((free_mem+cached_mem))
    #echo $free_mem
    if [ $free_mem -lt $mem_lmt ]
    then
    /etc/init.d/mysqld restart
    /etc/init.d/httpd restart
    echo “Apache and MySQL restarted on `date +’%Y-%m-%d %H:%M:%S’`. Available RAM is ${current}%” >>/var/log/memchecker.log
    echo “Apahache eating up RAM caused this service restart”| mail -s “Apache restarted ” email@domain.com
    #else
    #echo “memory status is safe and free memory is “$free_mem “MB”
    fi

    Also, like you, I comment out the else conditional since I have this hooked up to a cron job that would send me an email if there was output.

    Cheers!

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>