Mastering the Art of Reading Thread Dumps

Shailendra Bhatt
3 min readNov 29, 2023

I have been for years trying to find a structured way to read thread dumps in production whenever there is an issue. I have often found myself in a situation like finding a needle in a haystack and deciphering the cryptic language of thread dumps. These snapshots of thread activities within a running application have so much information, providing insights into performance bottlenecks, resource contention, and High Memory/CPU.

In this article, I’ll share my tips and tricks based on my experience, having read several production thread dumps effectively across multiple projects, demystifying the process for fellow expert engineers.

Tip 1: Understand the Thread States

Thread states, such as RUNNABLE, WAITING, or TIMED_WAITING, offer a quick glimpse into what a thread is currently doing. Mastering these states helps in identifying threads that might be causing performance issues. For instance, a thread stuck in a WAITING state can be a candidate for further investigation.

Tip 2: Identify High CPU Threads

The threads consuming a significant amount of CPU time are often the culprits behind performance degradation. Look for “Top 5 Threads by CPU time” threads and dig into their stack traces. It is where the full stack trace is defined, pinpointing the exact method or task responsible for the CPU spike.

Tip 3: Contextualize with Application Logs

Thread dumps are more powerful when correlated with application logs. Integrate logging within critical sections of your code to capture additional context. This fusion of thread dump analysis and log inspection (looking at the timestamps) provides a holistic view of the application’s behavior.

Tip 4: Pay Attention to Deadlocks

Deadlocks are the nightmares of multithreaded applications. Thread dumps provide clear indications of deadlock scenarios. Look for threads marked as “BLOCKED” and investigate their dependencies to identify the circular dependencies causing the deadlock.

Tip 5: Explore External Dependencies

Modern applications often rely on external services or APIs. Threads waiting for responses from these external dependencies can significantly impact performance. Identify threads in WAITING states and trace their dependencies to external services.

Tip 6: Utilize Profiling Tools

While thread dumps offer a snapshot of the application state, profiling tools like VisualVM, YourKit, or jVisualVM provide a dynamic and interactive way to analyze thread behavior. These tools allow you to trace thread activities in real time, making it easier to pinpoint performance bottlenecks.

Tip 8: Leverage Thread Grouping

Grouping threads by their purpose or functionality can simplify the analysis process. In complex applications, the number of threads can be really confusing. Hence, collating or grouping them together can be helpful. For example, grouping threads related to database connections, HTTP requests, or background tasks together. This approach often provides a more coherent view of the application’s concurrent activities.

In conclusion, reading thread dumps is both an art and a science. It requires a keen eye, a deep understanding of the application’s Architecture, and the ability to connect the dots between threads and their activities. It took years for me to get better at it, but it’s a skill that will always be helpful in troubleshooting any application.

Originally published at https://www.shailendrabhatt.com.

--

--

Shailendra Bhatt

Sr Architect- The stories are purely my personal views and not reflective of any organizations that I've been part of. Follow me https://www.shailendrabhatt.com