how to get thread and heap dump

Hi Techrunnr Readers, this document deals with how to get thread and heap dump.
A heap dump is a snapshot of the memory of a Java™ process. The snapshot contains information about the Java objects and classes in the heap at the moment the snapshot is triggered. Because there are different formats for persisting this data, there might be some differences in the information provided.

A thread dump is a snapshot of the state of all threads that are part of the process. The state of each thread is presented with a so-called stack trace, which shows the contents of a thread’s stack. Some of the threads belong to the Java application you are running, while others are JVM internal threads

Basically, thread and heap dumps are used for analyzing running java process, for improving the performance. This provides the full details about all the calls and status of those calls.

Here is a small shell script to get thread and heap dump of a process.

Create shell script file,

vi threadHeap.sh

add the following lines to created file.

#!/bin/bash
/bin/echo -e "\e[1;31mThread and Heap Dump Script\e[0m"
todate=$(date +"%Y-%m-%d-%H")
ps -aux|grep java
/bin/echo -e "\e[1;31mWhich Java Process ID you need to take heapDump and threaddump\e[0m"
#echo "Which Java Process ID you need to take heapDump"
read proid
echo "You selected the following Pid"
echo $proid
ja=$JAVA_HOME
$ja/bin/jmap -dump:format=b,file=/tmp/heapdump-001$todate.hprof $proid
thread=`cat /proc/$proid/status | grep Threads | awk '{print $2}'`
$JAVA_HOME/bin/jstack $proid > /tmp/threaddump-$thread-$todate.log

echo $?

if [ "$?" = "0" ]; then
	echo -e " \e[32mHeap Dump and heap dumo got sucessfull. Check the file in /tmp/heapdump-001$todate.hprof & /tmp/threaddump-$thread-$todate.log "
else
         echo "Dump failed" 
        exit 1
fi
Heapdump and Threaddump.sh

Make the script executable

chmod +x threadHeap.sh

Execute the script

sh thredHeap.sh

[ubuntu@localhost ~]$ sh threadHeap.sh
Thread and Heap Dump Script
Warning: bad syntax, perhaps a bogus ‘-‘? See /usr/share/doc/procps-3.2.8/FAQ
user1 20751 0.9 3.1 7959308 1027172 ? Sl Jul19 154:39 /usr/java/jdk1.7.0_79/bin/java -Djava.util.logging.config.file=/opt/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms1g -Xmx3g -Xss256k -XX:PermSize=256m -XX:MaxPermSize=256m -XX:+DisableExplicitGC -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=7171 –

Which Java Process ID you need to take heapDump and threaddump
20751
You selected the following Pid
20751
Dumping heap to /tmp/heapdump-0012018-07-30-03.hprof …
Heap dump file created
0
Heap Dump and heap dumo got sucessfull. Check the file in /tmp/heapdump-0012018-07-30-03.hprof & /tmp/threaddump-239-2018-07-30-03.log

Now you have taken thread dump and Heap Dump in /tmp location

© 2018, Techrunnr. All rights reserved.

#1
Questions Answered
Articles Written
Overall Points

Prabhin Prabharkaran

He is Technical professional. He is a person who loves to share tricks and tips on the Internet. He Posts what he does!!

1 Comment

Poomalar Selvan · July 30, 2018 at 4:39 pm

Nice guy Prabhin is. I really enjoyed working with him, especially in Linux area. Man got huge interest and knowledge on what he does.

Leave a Reply

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.