Get deadlock detection from running programm or dump in Java
Asked Answered
Y

3

5

I have a piece of running java software that is jammed. I would like to get a view inside but have got no idea how to do that.

Is there some tool that I can give a PID and it will tell me where every thread is currently located and maybe a few values of the variables as well? I'm running linux.

I more or less know what is causing the problem, but there a still a few possible cases, therefor pinpointing it would be nice.

I can't reproduce the error because it only appears every few days and never appeared while debugging, so this is a unique change of getting to know the enemy.

Any ideas?

Yammer answered 14/11, 2011 at 19:4 Comment(1)
Can you switch on remote debugging, for next time? i.e. #139011Cavern
I
12

Actually you can try to use visualvm + its threads monitoring plugin. You will be also able to make a thread dump, view thread stack traces ant their states. You can also use jconsole to detect deadlocks. Both tools are part of JDK. JConsole

Here is some more info on using visualvm for thread analysis.

Intermarry answered 14/11, 2011 at 19:16 Comment(0)
X
4

You can take a thread dump of it. You can use kill -3 PID where PID is your process ID. This will cause the thread dump to be output to the standard output of your program.

This will show you what every thread is doing, but will not give you any info regarding the variables. Regardless, thread dumps are really useful. I would start there. If you still can't fix the problem, you can use something like jmap (JVM tool, free but harder to use) or YourKit (paid product but very good) to take a memory snapshot, and examine the variables.

Some info on jmap: Java memory profiling with jmap and jhat

Xeres answered 14/11, 2011 at 19:8 Comment(3)
kill -3 PID doesn't seem to do anything. The JVM is still alive afterwards aswell. How can that be?Yammer
kill -3 won't actually kill the JVM, it just sends a signal to trigger a thread dump. The thread dump will go to the standard output of the JVM (not the std output of the kill command). Just make sure you're capturing the std output to a file. How are you running your program? Is it a standalone app, or are you running inside an app server like Tomcat? If for example, you're using Tomcat, the std output is in logs/catalina.out. If standalone, you can pipe your std ouput to some file by starting your app with something like: "java -jar myjar.jar > output.log". Hope this helps.Xeres
In your standalone case, what if you can't rerun the application, because it's production and it's already running, how can you direct your thread dump to a file?Kiddy
T
0

On recent JVMs (OpenJDK/Oracle Java 7 or above), if you take a heap dump (using either VisualVM or jmap), it also includes a dump of the stacks of all currently running threads, with links to the corresponding objects in the heap. You can then view the stacks by opening the heap dump in VisualVM.

Turtleneck answered 4/7, 2017 at 17:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.