Performance and scalability: Generating Java Full Thread Dump
Hi All, I want to generate full thread dump output(like kill -3) inside JVM. First of all , is it possible (in 1.5 or older)? Is there any code example? How JVM generate this output, when kill -3 is triggered? It uses jni? How can use it in a java class? I can generate partial of this output like using for deadlock, ThreadMXBean mbean = ManagementFactory.getThreadMXBean(); long ids = mbean.findMonitorDeadlockedThreads(); and for stacetrace, Map thrdMap = Thread.getAllStackTraces(); But output is not same.I want to use output to see in thread dump analyzers(it must be well-defined format). Thread.getAllStackTraces()doesnt contain - waiting to lock (a java.lang.Object). Thanks, Serdar
- Posted by: serdar mustaoglu
- Posted on: November 20 2006 07:18 EST
- Re: Generating Java Full Thread Dump by arijit dey on November 21 2006 00:28 EST
- Re: Generating Java Full Thread Dump by Tim Eck on November 24 2006 11:38 EST
- Re: Generating Java Full Thread Dump by Virag Saksena on November 28 2006 09:56 EST
- Generating Java Full Thread Dump by Cédrik LIME on July 22 2011 11:19 EDT
- We do this by Daniel Doubleday on September 14 2011 12:10 EDT
You can call Thread.dumpStack().This will give you the stack trace for the current thread.And please avoid duplicating you posting, we anyway look at all the categories. cheers, http://javaicillusion.blogspot.com/
You can use jstack to take a dump.
You would think there would be an easy way to do this.... The one method that works with all Sun VMs (so far) is to write a little JNI lib so that you can get the process ID of your VM (e.g on linux, call getpid()). Then just run kill -3 , or on windows use SendSignal.exe (google for that executable). Unfortunately the thread dump is going to be on stderr, so you had better know where that stream is going (hopefully not /dev/null) With 1.5, a combination of the jps and jstack commands can help you to accomplish the same thing. note: jstack isn't in the windows JDK until 1.6 for some reason. Jrockit might have some special magic for doing thread dumps, I don't know much about it. They might also be some solaris specific things to let you look at thread states. If you're running within Weblogic, I think they include a class for doing exactly this too, although you won't find it adverised as a public feature afaik. Anyway, for the number of platforms and VM versions I have to deal with, the first approach (JNI for process ID plus an OS specific signal program seems) to work best.
At least in 1.6 the thread dump generated by Ctrl+Break in Windows goes to stdout.
What are you trying to do ? Depending upon the overhead you are willing to incur, you can enable JVMPI/JVMDI (with pre-1.5) or JVMTI(with 1.5) to get more access to some of the stack trace functions. Another option (if you don't want the overhead) is to use Auptyma's Java Application Monitor. It is an asynchronous thread stack sampler for finding Java performance issues. It will provide you stack (and contended object) information. Regards, Virag http://www.auptyma.com
And for when you don't have ssh access to your server, there's only one solution: MessAdmin http://messadmin.sourceforge.net. As a bonus, you will also get plenty of technical information on you server! :-)
ProcessBuilder processBuilder = new ProcessBuilder("/bin/sh", "-c", "kill -3 $PPID");
Process process = processBuilder.start();
InputStream inputStream = process.getInputStream();