Saturday, September 14, 2013

Heap dump on a Unix machine

After exploring the Jay/Faisal's blog on 'jmap' java utility usage and Heap dump. It is really great work by Jay/Faisal. I just thought that similar kind of experiment we did long back on UNIX machines. I am glad to sharing that with you guys. 

Last year, we were struggling to overcome the OutOfMemoryError, which would effect the most of productive hours. In this assignment I need to figure out what process is causing the low memory in the environment is identified by searching a all log files in the machine. Assume that all the WebLogic instance log files are collected into common directory structure, each of them are stored respective instance named folder.

Script 1:
Data provided by Pastebin.com - Download Raw - See Original
  1. #==============================================================
  2. # Name  : CheckLogs.sh
  3. # Created by : Pavan Devarakonda
  4. # Script for searching all WebLogic instances logs in the box
  5. #==============================================================
  6. insts=`ls /path/instances|grep web`
  7. phrase=$1
  8. date
  9. for x in $insts
  10. do
  11.         echo 'Checking in the :' $x
  12.         cd /path/instances/$x/logs
  13.         egrep -$phrase instance*log
  14.         egrep -$phrase instance*out
  15. done
  16.  
  17. # Know the CPU load average at this time
  18. date
  19. uptime


After identifying the impacted instances, I need to take the heap dump of that particular instance with corresponding process id.

Script 2:
Data provided by Pastebin.com - Download Raw - See Original
  1. #=======================================================
  2. # Name : InstanceHeapdump.sh
  3. # This script will takes instance name as input
  4. # Takes the thread dump and also takes heap dump
  5. #=======================================================
  6.  
  7. if [ "$1" = "" ]then
  8.         echo "Usage : $0 <instance>"
  9.         exit
  10. else
  11.         instance="$1"
  12.         user=$LOGNAME
  13.         ppid=`ps -fu $user|grep $instance|grep startMan|grep -v grep|awk '{print $2}'`
  14.         wpid=`ps -fu $user|grep $ppid|grep startWebLogic.sh|awk '{print $2}'`
  15.         jpid=`ps -fu $user|grep $wpid|grep java|awk '{print $2}'`
  16.         echo 'The Java PID:' $jpid
  17.         kill -3 $jpid
  18.         if [ $jpid = "" ]then
  19.                 echo "Invalid instance input..."
  20.                 exit
  21.         else
  22.                 jmap -heap:format=b $jpid
  23.                 mv heap.bin $instance_heap.bin
  24.         fi
  25. fi



This could give you one more way of finding a java process in UNIX machine. You can use jps command instead of three lines of awk filters. In this same script to make hold the java process not to crash, we can call a WLST script to suspend the instance and then you can happily take the heap dump.

What to do after heapdump creation?
Follow the Faisal tips to find memory leaks, use eclipse MAT that is comfortable for your system.

No comments:

Post a Comment