What is Thread Dump?
The thread dump is a snapshot of exactly what’s executing at a moment in time in your Java Program.
While the thread dump format and content may vary between the different Java vendors, at the bare minimum it provides you a list of the stack traces for all Java threads in the Java Virtual Machine.
Using this information, you can either analyze the problem
yourself, or work with those who wrote the running code to analyze the problem.
Thread dump is just a list of all threads and the full stack trace of code running on each thread. If you are a J2EE Application Server administrator and you’ve never done development before, the concept of a stack trace may be foreign to you.
A stack trace is a dump of the current execution stack that shows the method calls running on that thread from the bottom up.
Here is a sample Thread Dump:
"crunchifyThread3" java.lang.Thread.State: RUNNABLE at sun.management.ThreadImpl.getThreadInfo1(Native Method) at sun.management.ThreadImpl.getThreadInfo(ThreadImpl.java:174) at com.crunchify.tutorials.CrunchifySynchronizeThread.getThreadDump(CrunchifyThreadDeadLock.java:64) at com.crunchify.tutorials.CrunchifySynchronizeThread.run(CrunchifyThreadDeadLock.java:50) at java.lang.Thread.run(Thread.java:722) "crunchifyThread2" java.lang.Thread.State: BLOCKED at com.crunchify.tutorials.CrunchifySynchronizeThread.run(CrunchifyThreadDeadLock.java:53) at java.lang.Thread.run(Thread.java:722) "crunchifyThread1" java.lang.Thread.State: BLOCKED at com.crunchify.tutorials.CrunchifySynchronizeThread.run(CrunchifyThreadDeadLock.java:53) at java.lang.Thread.run(Thread.java:722) "Signal Dispatcher" java.lang.Thread.State: RUNNABLE "Finalizer" java.lang.Thread.State: WAITING at java.lang.Object.wait(Native Method) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151) at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:189) "Reference Handler" java.lang.Thread.State: WAITING at java.lang.Object.wait(Native Method) at java.lang.Object.wait(Object.java:503) at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:133)
Java Method to Generate Thread Dump:
/** * @author Crunchify.com * */ public static String crunchifyGenerateThreadDump() { final StringBuilder dump = new StringBuilder(); final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); final ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds(), 100); for (ThreadInfo threadInfo : threadInfos) { dump.append('"'); dump.append(threadInfo.getThreadName()); dump.append("\" "); final Thread.State state = threadInfo.getThreadState(); dump.append("\n java.lang.Thread.State: "); dump.append(state); final StackTraceElement[] stackTraceElements = threadInfo.getStackTrace(); for (final StackTraceElement stackTraceElement : stackTraceElements) { dump.append("\n at "); dump.append(stackTraceElement); } dump.append("\n\n"); } return dump.toString(); }
In my previous Java Deadlock Example I’ve used the same method to generate thread dump. Here is a list of all Java Examples and Spring MVC Examples which you may be interested in.
Want to generate memory Heap Dump on OOM?
Please use -XX:+HeapDumpOnOutOfMemory
parameter while starting java application.