What is thread dump in java?.
A thread dump is a snapshot of all the Java threads that are currently active(whether blocked , running,deadlocked,waiting,suspended,runnable) in a Java Virtual Machine (JVM).
Why we need Thread Dump?.
if CPU usage is very high due to running java program/application
if Processing performance of a java program/application is Slow
if Application is not responding.
How to take Thread Dump?.
Below are the step by step process.
Step 1: Get PID(processID) of java process.
JDK ships with jps command exe which lists all java process ids.
There are multiple way to get the pid..
root>jps
22026 Jps
8555 Bootstrap (my tomcat server)
root>jps -l
29928 sun.tools.jps.Jps
8555 org.apache.catalina.startup.Bootstrap (my tomcat server)
root>jps -v
13861 Jps -Dapplication.home=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.75.x86_64
8555 Bootstrap -Xms512m -Xmx1024m -XX:MaxPermSize=256m (my tomcat server)
UNIX/Linux Users:
ps -ef | grep java
ps -ef | grep java | awk '{print $2}' ... it will give you PID only.
ps -fC java
Windows Users:
Press Ctrl+Shift+Esc to open the task manager and find PID of java process.
jps will work in all enviroment( unix/windows).
there are other way also to take thread dump..
a.JVisualVM if you are able to run this becoze sometime you have limited access on production server.
b.if you want to see/view the threads of a java process instead of thread dump.
c.JConsole exe tool.
d.programmaticly using Mbean--ManagementFactory.getThreadMXBean() ,
Step 2: Take a Thread Dump from the JVM.
You can use
kill -QUIT
or kill -3
command command and get threadDump of java process @Linux OS.
kill -
3
<pid>
JDK ships with jstack command exe which can be used for taking thread dump of java process.
jstack pid-19327 >> ./app-root/logs/threaddumps-poe-2.log
here is sample for reference.
Full thread dump OpenJDK 64-Bit Server VM (24.65-b04 mixed mode):
"hz._hzInstance_1_dev.cached.thread-2615" prio=10 tid=0x00007f420400a800 nid=0x2d3f waiting on condition [0x00007f42731c5000]
java.lang.Thread.State: TIMED_WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000000c190e018> (a java.util.concurrent.SynchronousQueue$TransferStack)
at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:226)
at java.util.concurrent.Synch ronousQueue$TransferStack.awaitFulfill(SynchronousQueue.java:460)
at java.util.concurrent.SynchronousQueue$TransferStack.transfer(SynchronousQueue.java:359)
at java.util.concurrent.SynchronousQueue.poll(SynchronousQueue.java:942)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1068)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
"Attach Listener" daemon prio=10 tid=0x00007f4250002000 nid=0x713e waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"MultiThreadedHttpConnectionManager cleanup" daemon prio=10 tid=0x00007f42242c7800 nid=0x4c07 in Object.wait() [0x00007f42730c4000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135)
- locked <0x00000000c26a4da0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151)
at org.apache.commons.httpclient.MultiThreadedHttpConnectionManager$ReferenceQueueThread.run(MultiThreadedHttpConnectionManager.java:1122)
"[DSVQuartzThreads]" prio=10 tid=0x00007f422404f800 nid=0x4c95 in Object.wait() [0x00007f42635f4000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
at org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:410)
- locked <0x00000000c1dc5818> (a java.lang.Object)
Use IBM thread analyzer runnable gui jar(jca457.jar) for opening the thread Dump and see all the information->
ftp://public.dhe.ibm.com/software/websphere/appserv/support/tools/jca/jca457.jar