0

I am trying something like this.I have a application server I want to know is there a way to collect the jvm details like thread deadlock before graceful shutdown.I can use ShutDownHook but in shut down hook how to collect the jvm details like deadlock etc.

Thanks

chiru
  • 812
  • 5
  • 17
  • 32
  • possible duplicate of [Generate a Java thread dump without restarting.](http://stackoverflow.com/questions/12842344/generate-a-java-thread-dump-without-restarting) – assylias Jan 29 '14 at 11:43
  • But deadlock is not a piece of information, it is a runtime state. The information you would be able to get is the thread dumps, and from that you might reason that there is a deadlock. – Gimby Jan 29 '14 at 11:46
  • 1
    @Gimby [`ThreadMXBean#findDeadlockedThreads`](http://docs.oracle.com/javase/6/docs/api/java/lang/management/ThreadMXBean.html#findDeadlockedThreads%28%29) can detect deadlocks. – assylias Jan 29 '14 at 11:51
  • Magic. I stand corrected. – Gimby Jan 29 '14 at 12:07
  • I am trying to do programmatically not using any tool!May be assylias is giving pointer – chiru Jan 29 '14 at 12:47
  • I'd still say use jstack. You can do a Runtime.exec() to launch it from your ShutdownHook. – Ted Bigham Feb 04 '14 at 22:22

2 Answers2

1

Does it need to be executed from inside java? Externally you can use jstack.

Ted Bigham
  • 4,237
  • 1
  • 26
  • 31
1

jstack is platform dependent and it is not available on all platforms. Therefore jstack may not be useful under all situations.

It is not possible to create full JVM thread dump from within JVM. You may call JVM_dumpAllStacks function on JVM DLL to create thread dump. It depends on whether that route is acceptable for you because it involves JNI and some native coding. Look at Open JDK Bug list for some more information and knowing efforts to get this functionality from java management APIs. Other JVMs may also follow this route in future. In that case you will be able to invoke JVM thread dump through management API.

At this time only option you may have (when you don't want to take JNI route) is creating thread dumps using good old methods like this: Creating a Thread Dump

RaviH
  • 3,544
  • 2
  • 15
  • 14