When your application experiences slowdowns, hangs, deadlocks, high CPU usage, or any other performance anomalies, these issues are reflected in the behavior of the application’s threads. By studying thread behavior, you can quickly triage and identify the root cause of performance problems. Analyzing a thread dump is the most effective & non-intrusive way to study the thread’s behavior. In this post, I’ll explain what a thread dump is, the valuable information it provides, and how you can analyze it effectively to resolve performance issues.
What is a Thread Dump?
Thread dump is a snapshot (i.e. photograph) of all threads that are running in the JVM. It contains a rich set of information around the threads, such as: Thread Name, Thread State, Thread Id, lines of code each thread in the application is executing, what locks does the thread hold, what locks it is waiting for… Thus, Thread dumps provide a comprehensive view of what every thread in the JVM is doing, making them a vital artifact for troubleshooting.
Information present in a Thread Dump
Thread dump can be divided into two main parts: the header and the body.
Header part of the thread dump, contains following information
1. TimeStamp: The Date and Time at which thread dump was captured.
2. JVM Version: Information about the JVM from which thread dump was captured. i.e. whether it’s a 32-bit or 64-bit JVM, whether it runs on client mode or server mode
The body part of the thread dump contains information about each thread in the application. For each thread following information will be present:
1. Thread Name: The name assigned to the thread. Developers can use the Thread#setName() API to set a custom name, which will be displayed here.
2. Priority: Threads can have a priority between 1 (lowest) and 10 (highest). In cases of CPU contention, higher-priority threads are given preference. Note: This is only a suggestion provided by the JVM to the OS kernel, which may or may not honor it. Most kernels, in fact, don’t strictly follow it. 😊
3. Thread Id: A unique identifier assigned by the JVM to each thread.
4. Native Id: At the end of the day, thread is an Operating System resource. Thus, to keep track of its resources, this is the unique Id assigned by the OS to the thread. If you are using Operating System tools such as top, pmap, … to study the threads behavior, the threads will be referenced with this Id.
5. Address Space: The memory space in which the thread operates. This information is usually not needed for troubleshooting, so you don’t have to worry about it.
6. Thread State: Indicates the current state of the thread. A thread can be in 6 different thread states: NEW, TERMINATED, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING. Watch our video clip to learn more about Java thread states.
7. Stacktrace: This is the code execution path of the thread. It should be read from the bottom to top. The line at the bottom is the first line of code executed and the line at the top is the most recently executed line of code.
How to Capture Thread Dump?
A Java thread dump can be captured by using the ‘jstack’ command line tool which is present in JDK. Here is the jstack command that you need to issue to capture thread dump:
jstack -l <pid> > <file-path>where
pid: is the Process Id of the application, whose thread dump should be captured
file-path: is the file path where thread dump will be written to.
Example:
jstack -l 1720 > /opt/myapp/threadDump.txtAs per the example thread dump of the process would be generated in /opt/myapp/threadDump.txt file.
Besides ‘jstack’ there are 8 different options to capture thread dump. You can use the option which suits your organization’s security concerns and availability.
Best Practice for Capturing Thread Dumps
A thread dump is a snapshot of all the threads running in your application at a specific moment. To determine whether a thread is genuinely stuck or just momentarily paused, it’s essential to capture multiple snapshots at regular intervals. For most business applications, capturing three thread dumps at 10-second intervals is a good practice. This approach allows you to observe if a thread remains stuck in the same code line across multiple snapshots.
Learn best practices for thread dump capturing in our post.
How to Analyze Thread Dumps?
Thread dump formats are not standardized, they vary from JVM version, vendor and tool you use for capturing it. Thus, you can use tools like fastThread, yCrash which can analyze all different formats of thread dumps. Tool will instantly analyze thread dumps by applying various Machine Learning algorithms and report the root cause of the performance problem. Here is a sample thread dump analysis report generated by the fastThread tool. By using tools like fastThread, you can quickly pinpoint the root cause of performance problems without having to manually sift through complex stack traces. This can save hours of troubleshooting time and help maintain application stability.
Conclusion
In summary, a thread dump is an essential artifact that provides a comprehensive snapshot of all threads in the JVM, revealing valuable information such as thread state, execution path, and resource contention. By capturing and analyzing thread dumps, you can gain deep insights into your application’s performance issues and quickly pinpoint the root cause of problems. Tools like fastThread make this process even easier by automating the analysis and delivering actionable insights.




4 Pingback