Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a thread dump via JMX?

I have a Tomcat running as a Windows Service, and those are known not to work well with jstack. jconsole is working well, on the other hand, and I can see stacks of individual threads (I'm connecting to "localhost:port" to access it).

How can I use jconsole or a similar tool to dump all the thread stacks into a file? (similar to jstack)

like image 238
ripper234 Avatar asked Oct 18 '10 12:10

ripper234


People also ask

How do you create a thread dump?

Generating thread dumps on windowsClick the console window and press <CTRL>+BREAK (or SHIFT+CTRL+PAUSE on some keyboards). The thread dump will print directly to the console.

What is thread dump in performance testing?

A thread dump is a snapshot of all threads running in the application at a point in time. It contains all the information about each thread in the application such as thread state, thread Id, native Id, thread name, stack trace, and priority.


4 Answers

You can use the ThreadMXBean management interface.

This FullThreadDump class demonstrates the capability to get a full thread dump and also detect deadlock remotely using JMX.

like image 56
dogbane Avatar answered Sep 24 '22 12:09

dogbane


Nowadays you can use jvisualvm tool to connect to your remote JVM through JMX and create a thread dump. Don't know if this was available

Java VisualVM

like image 29
Claudio Avatar answered Sep 25 '22 12:09

Claudio


Here's another code sample that will write a stack dump to a file:

http://pastebin.com/zwcKC0hz

We use this over JMX to give us an approximation of the stack dump you get when you make a JMX request or if the process detects high, unexpected load.

like image 30
Gray Avatar answered Sep 24 '22 12:09

Gray


It would be helpful if you take a flight recording to get a deeper view on the JVM behavior, specially focusing on the Hot Methods.

Usually, a recording of half an hour is enough. To trigger a recording, you must be logged in to the machines, and issue the following command:

If using Java HotSpot 1.8.x:

$JAVA_HOME/bin/jcmd VM.unlock_commercial_features $JAVA_HOME/bin/jcmd JFR.start duration=1800s settings=profile filename=/tmp/recording.jfr

IF using java HotSpot 1.7.x:

Edit your $HOME/conf/wrapper.conf file by adding the following parameters on JVM startup:

wrapper.java.additiona.=-XX:+UnlockCommercialFeatures wrapper.java.additional.=-XX:+FlightRecorder

(replace with the corresponding positional number )

Then, have your instances restarted. Once done, issue the following command :

$JAVA_HOME/bin/jcmd JFR.start duration=1800s settings=profile filename=/tmp/recording.jfr

The flight recording wil produce a file on /tmp/recording.jfr upon termination.

like image 38
Sanjeet Pandey Avatar answered Sep 25 '22 12:09

Sanjeet Pandey