开发者

java stack dump on windows

I have a running java process in a standard windows command window. ie i have run 'cmd' and typed in java -jar ...

I need to be able to get a full stack dump of all threads if at all possible.

i remember that under linux you can send a message to the JVM via an option on the quit command.

in this document sun state

To generate a stack trace on Windows 95, or Windows NT platforms, enter the key sequence in the window where the Java program is running, or click the Close button on the window.

this is clearly wrong, a开发者_开发百科s closing the terminal does nothing but kill the process and close the window.


You can use jstack [ option ] pid (if the question is about thread dump). Use jps to find the id of your Java process.


Typing Ctrl+Break is the correct way to generate a thread dump on Windows.

Are you pressing Ctrl+C (=interrupt) maybe? That will send a SIGINT, which will generally kill your process.


In the Java 6 JDK+ the jvisualvm executable allows you to attach to a running program (double click on its entry in the left side).

When attached, there is a Threads pane on the right side, which has a Thread Dump button.

This gives you a thread dump.

Once generated you can A) Select all - Copy and paste the thread dump to a text editor. or B) you can Right click on the thread dump created in the tree on the left hand side and say "Save as".

Notes jvisualvm also allows you to take snapshots of the whole application for later analyzis.


This may help you but also depends on which JVM version and provider you are using.

http://java.sun.com/developer/technicalArticles/J2SE/monitoring/

http://java.sun.com/javase/6/docs/technotes/tools/share/jstack.html

http://java.sun.com/javase/6/docs/technotes/guides/visualvm/index.html


In addition to Seth's answer, you could also launch a JConsole on your machine in order to inspect each thread's stack trace. This has the added benefit of being able to detect deadlocks and monitor memory usage.


Other posters are correct - ctrl-break in the console or jstack.

Otherwise, if you're using Java 1.5 or above, you can get hold of this information programmatically at runtime, via:

Thread.getAllStackTraces() (http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html#getAllStackTraces())

then iterate through the results.

A little long-winded, but something like this:

Map<Thread,StackTraceElement[]> traces = Thread.getAllStackTraces();
Iterator<Thread> i = traces.keySet().iterator();
while (i.hasNext()) {
   Thread thd = i.next();
   System.out.println("*** Thread id"+thd.getId()+":"+thd.getName()+" ***");
   StackTraceElement[] trace = traces.get(thd);
   for (int j=0; j < trace.length; ++j) {
      System.out.println(trace[j]);
   }
   System.out.println();
}

You can then choose whatever method you want to trap your program to cause this to happen, and format/direct the output.

A disadvantage of this method is that it isn't exact, in that the stacks of the different threads aren't guaranteed to have been taken at exactly the same time. However, there's an associated performance advantage in that your entire process won't be suspended while the snapshot is taken.


Take a look at this Stack Overflow post

Thread dump programmatically /JDI (Java Debugger Interface)

We have implemented a JMX method to dump stack traces. This is really handy because you can examine stack traces in a web browser even on a remote machine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜