Heap Dump JMAP Script
In this blog post, we are going to write a shell script which is going to read the values from a properties file and generate the heap dump via JMAP.
#!/bin/bash
#################################################
#
# Name : Heap Dump Generator Script
# Author : Togotutor
# Copyright @2012 www.togotutor.com
#
################################################
echo '*** Shell Script To Capture Heap Dump Of a Java Process ***'
JAVA_HOME=`cat env.properties | grep JAVA_HOME | sed -e 's|JAVA_HOME||g' | sed -e 's|export||g' | sed -e 's|=||g'`
PORT=`cat env.properties | grep PORT | sed -e 's|PORT=||g'`
SERVER_NAME=`cat env.properties | grep SERVER_NAME | sed -e 's|SERVER_NAME=||g'`
PID=`ps -ef | grep java | grep ${SERVER_NAME} | awk '{print $2}'`
FILENAME=`cat env.properties | grep FILENAME | sed -e 's|FILENAME=||g'`
echo "----------------------------------"
echo "JAVA_HOME: ${JAVA_HOME}"
echo "PORT: ${PORT}"
echo "SERVER_NAME: ${SERVER_NAME}"
echo "PROCESS ID: ${PID}"
echo "FILENAME: ${FILENAME}"
echo "----------------------------------"
if [ -f ${FILENAME} ]; then
echo "Old file present: backing up the old file"
mv ${FILENAME} `date +%Y_%m_%d_%H:%M:%S`-${FILENAME}.bin
else
echo "No Old Files found"
fi
${JAVA_HOME}/bin/jmap -dump:format=b,file=heapmap.bin ${PID}
To use this script just copy and paste the above content to a .sh file.
Create another file with the name env.properties. Here we are going to add/modify the value specific to the environment.
################################################# # # Name : Environment Properties File # Author : Togotutor # Copyright @2012 www.togotutor.com # ################################################ JAVA_HOME=/home/oracle/Middleware/jdk PORT=8001 SERVER_NAME=AdminServer FILENAME=heapmap.bin
Save both the files and execute the script ./heap_dump.sh
You will see the following output:
*** Shell Script To Capture Heap Dump Of a Java Process *** ---------------------------------- JAVA_HOME: /home/oracle/Middleware/jdk PORT: 8001 SERVER_NAME: AdminServer PROCESS ID: 25404 FILENAME: heapmap.bin ---------------------------------- Old file present: backing up the old file Dumping heap to /home/oracle/Middleware/user_projects/domains/togodomain/scripts/heapmap.bin ... Heap dump file created
Once the script exists successfully. A file will be created with the dump heapmap.bin. Any other old file with the same name will be backed up with date time stamp.
You can download the source from here.

