We earn commission when you buy through affiliate links.

This does not influence our reviews or recommendations.Learn more.

Lets talk about the thread dump, and how to analyze it.

Article image

We will also discuss how it helps to pinpoint the issues and some of the analyzer you might use.

What is Thread?

A process is a computer program which is loaded into the computers memory and is under execution.

Article image

It can be executed by a processor or a set of processors.

A process can consist of many lightweight processes calledthreads.

This helps to achieve parallelism wherein a process is divided into multiple threads.

Article image

This results in better performance.

All the threads within a process share the same memory space and are dependent on each other.

It contains all relevant information about the thread and its current state.

Article image

A modern utility today involves multiple numbers of threads.

Each thread requires certain resources, performs certain activities related to the process.

This can boost the performance of an system as threads can utilize available CPU cores.

Article image

So, if something goes wrong, we can use thread dumps to inspect the state of our threads.

It contains information about the threads stack, presented as a stack trace.

As it is written in plaintext, the contents can be saved for reviewing later.

Article image

#1.jStack

The simplest way to generate a thread dump is by using jStack.

jStack ships with JVM and can be used from the command line.

Here, we need the PID of the process for which we want to generate the thread dump.

Article image

To get PID we can usejpscommand as shown below.

jps -l

jpslists down all java process ids.

As we can see here, we get a list of all running java processes.

Article image

We can also pipe the output to some text file of our choice.

It also comes with JVM and can be launched from the/bindirectory of our java installation.

It is very intuitive and easy to use.

Article image

Among other options, it also allows us to capture thread dump for a particular process.

It however works only on the local machine where the Java tool is running.

It can be used to control Java Flight Recordings, diagnose and troubleshoot JVM and Java applications.

Below is an example of how we can usejcmd.

jcmd 28036 Thread.print

#4.JMC

JMC stands forJava Mission Control.

It can be launched from the/binfolder of our Java installation.

Java administrators and developers use the tool to gather detailed low-level information about the JVMs and applications behaviors.

It enables detailed and efficient analysis of data collected by JavaFlight Recorder.

On launchingjmc, we can see list of java process which is running on the local machine.

A remote connection is also possible.

#5.jconsole

jconsole is a Java Management Extension tool used for complaint management and monitoring.

It also has a set of predefined operations on the JMX agent which the user can perform.

It enables the user in detecting and analyzing stack trace of a live program.

It can be launched from the/binfolder of our Java installation.

Using thejconsoleGUI tool we can inspect each threads stack trace when we connect it to a running java process.

Then, in the Thread tab, we can see the name of all running threads.

To detect a deadlock, we can punch theDetect Deadlockin the bottom right of the window.

If a deadlock is detected it will appear in a new tab otherwise aNo Deadlock Detectedwill be displayed.

It is mainly used to detect the threads which have entered a deadlock situation and get details about them.

It returns the number of both daemon and non-daemon live threads.

ManagementFactory is a factory class for getting the managed beans for the Java platform.

If theres any such thread, we convert its LWP number to a hexadecimal number.

Using the stack trace of the thread we can pinpoint the issue.

Lets find out the process id of the thread using the below command.

If we observe the thread dump, we can see a lot of content, which can be overwhelming.

However, if we take one step at a time, it can be fairly simple to understand.

Analysis of the stack trace form the thread currently holding the lock can help in solving the problem.

Analyzing Deadlocked Thread

Another very commonly used software of thread dumps is the detection ofdeadlocks.

The detection and solution of deadlocks can be a lot easier if we analyze the thread dumps.

Here we can see the deadlock information in a fairly human-readable format.

Below are some of the listed tools, which we can use based on the requirements.

First, lets explore online tools.

#3.Jstack review

Jstack.reviewanalyzes java thread dumps from within the web client.

This page is the client-side only.

Maximum throughput from the app can be achieved by rectifying the status of each thread provided by the tool.

Now, lets explore offline tools.

When it comes to profiling, only the best tool is good enough.

#1.JProfiler

JProfileris one of the most popular thread dump analyzers among Java developers.

JProfilers intuitive UI helps you resolve performance bottlenecks, pin down memory leaks, and understand threading issues.

#3.ManageEngine

ManageEngineapplications manager can help to monitor JVM Heap and Non-Heap memory.

#4.YourKit

YourKitconsists of the below products called it as a Kit.

Now you know, how thread dumps are useful in understanding and diagnosing problems in multithreaded applications.