Thursday, June 2, 2016

Java : How to take Thread Dump in java OR usage of Jstack and VisualVM to take Thread Dump in Java

It is a very very important topic for a java developer who wants to prove to be a brilliant java developer with respect to multi-threading. It directly means that to become a multi-threading master mind you must know what is Thread Dump and how you can take Thread Dump in your project.

Suppose you are working with multi-threading and you are heavily using the synchronization in your code . Obviously, you will be using the wait(), notify() and notifyAll() methods. On the other you can also use concurrent API's for the same purpose. But while working with the synchronization in your project , it is highly recommended that your code should not cause any deadlock. Deadlock means when two threads are waiting for each other to release the lock they require.

And, if your project is using the synchronization so heavily that you are not able to find out the code which is causing the deadlock. Thread Dump is the relief for such situations. Thread Dump will give you the method and thread name which is causing the deadlock.

Thread Dump not only provides the information about the deadlock but tells the following information as well:

1) Performance Analysis: You can analyze where the application getting down and measure the performance by the help of Thread Dump.
2) Thread States: Can predict which thread is in which state(Start,Runnable,Dead etc.).
3) CPU consumption by the different different threads.
4) Garbage Collection information in detail.


Hence, this tool is very useful for the java family.

Now, we will proceed towards the procedure to take the Thread Dump. Let's move towards the cinema:

There are two ways to take the Thread Dump :
* By Using the jstack tool through command prompt (easiest way).
* By Using VisualVM tool through eclipse.

Let's go with jstack tool first:

1) Open task manager(alt+ctrl+del).
2) Go to processes tab.
3) Click on View menu and select "Select columns..." menu item.
4) Check the PID(Process Identifier) from the list and click on ok button.
5) PID would be added in the list of processes.
6) Copy the PID of javaw.exe process.
7) Open command prompt and go to the bin directory of jdk . like : C:\Program Files\Java\jdk1.7.0_79\bin
8) type the following command in command prompt:
jstack.exe -l PID     (PID is the copied PID from the task manager).
9) Press enter and Thread Dump is getting started.

You can analyse the Thread Dump generated in this way . You can also copy this Dump in any text file like with the following command:
jstack.exe -l PID > ThreadDump.txt
Where ThreadDump.txt is the file on the path : C:\Program Files\Java\jdk1.7.0_79\bin

Now, Let's go with VisualVM approach.

If we are following this approach then we need to install the VisualVM in the eclipse by using the following steps:

Download VisualVM Launcher(Eclipse 3.6+) depending on your eclipse version , extract downloaded zip file.
* Open eclipse and go to Help > Install new software
Click on Add
* Click on Local and browse the extracted launcher
* You may name it whatever you may like.Click Ok.
* Install this software like this way

Restart the Eclipse and follow these steps:

1) Go to windows > Preference > type VisualVM
2) Select visualVM executable location, Select Jdk available on your system and click OK.
3) Right click on class for which you want to run visualVm Launcher and want to analyse.
4) Go to Run As...and then select Run Configurations...
5) Select VisualVM Launcher.

Your full information about the threads causing deadlock and other information will be shown on the window opened in the eclipse. From here you can easily analyze the thread running process.

Happy Learning !!!

No comments:

Post a Comment