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?

Franz Kafka
  • 10,623
  • 20
  • 93
  • 149
  • Can you switch on remote debugging, for next time? i.e. http://stackoverflow.com/questions/138511/what-are-java-command-line-options-to-set-to-allow-jvm-to-be-remotely-debugged – laher Nov 14 '11 at 19:11

3 Answers3

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.

szhem
  • 4,672
  • 2
  • 18
  • 30
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

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
Andres Olarte
  • 4,380
  • 3
  • 24
  • 45
  • kill -3 PID doesn't seem to do anything. The JVM is still alive afterwards aswell. How can that be? – Franz Kafka Nov 14 '11 at 20:30
  • 3
    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. – Andres Olarte Nov 15 '11 at 15:21
  • 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? – J.E.Y Sep 30 '19 at 12:28
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.

James_pic
  • 3,240
  • 19
  • 24