Windows上的java堆栈转储
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2124672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java stack dump on windows
提问by pstanton
I have a running java process in a standard windows command window. ie i have run 'cmd' and typed in java -jar ...
我在标准 Windows 命令窗口中有一个正在运行的 java 进程。即我已经运行了“cmd”并输入了 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.
我记得在 linux 下,您可以通过退出命令上的选项向 JVM 发送消息。
in this documentsun 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.
要在 Windows 95 或 Windows NT 平台上生成堆栈跟踪,请在运行 Java 程序的窗口中输入密钥序列,或单击窗口上的关闭按钮。
this is clearly wrong, as closing the terminal does nothing but kill the process and close the window.
这显然是错误的,因为关闭终端只会终止进程并关闭窗口。
采纳答案by Seth
Typing Ctrl+Breakis the correct way to generate a thread dump on Windows.
键入Ctrl+Break是在 Windows 上生成线程转储的正确方法。
Are you pressing Ctrl+C(=interrupt) maybe? That will send a SIGINT, which will generally kill your process.
你是按Ctrl+ C(=interrupt) 吗?这将发送一个 SIGINT,这通常会终止您的进程。
回答by Pascal Thivent
You can use jstack [ option ] pid
(if the question is about thread dump). Use jps
to find the id of your Java process.
您可以使用jstack [ option ] pid
(如果问题是关于线程转储的)。使用jps
找到你的Java进程的ID。
回答by Gladwin Burboz
This may help you but also depends on which JVM version and provider you are using.
这可能对您有所帮助,但也取决于您使用的 JVM 版本和提供程序。
http://java.sun.com/developer/technicalArticles/J2SE/monitoring/
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/tools/share/jstack.html
http://java.sun.com/javase/6/docs/technotes/guides/visualvm/index.html
http://java.sun.com/javase/6/docs/technotes/guides/visualvm/index.html
回答by Adamski
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.
除了 Seth 的回答之外,您还可以在您的机器上启动一个 JConsole 以检查每个线程的堆栈跟踪。这具有能够检测死锁和监控内存使用情况的额外好处。
回答by user242275
Other posters are correct - ctrl-break in the console or jstack.
其他海报是正确的 - 在控制台或 jstack 中按 ctrl-break。
Otherwise, if you're using Java 1.5 or above, you can get hold of this information programmatically at runtime, via:
否则,如果您使用的是 Java 1.5 或更高版本,则可以在运行时通过以下方式以编程方式获取此信息:
Thread.getAllStackTraces()
(http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html#getAllStackTraces())
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.
这种方法的一个缺点是它不准确,因为不能保证不同线程的堆栈完全在同一时间被获取。但是,有一个相关的性能优势,因为在拍摄快照时您的整个过程不会暂停。
回答by Thorbj?rn Ravn Andersen
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).
在 Java 6 JDK+ 中,jvisualvm 可执行文件允许您附加到正在运行的程序(双击左侧的条目)。
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".
生成后,您可以 A) 全选 - 将线程转储复制并粘贴到文本编辑器。或 B) 您可以右键单击左侧树中创建的线程转储,然后说“另存为”。
Notes jvisualvm also allows you to take snapshots of the whole application for later analyzis.
注意 jvisualvm 还允许您拍摄整个应用程序的快照以供以后分析。
回答by Fortyrunner
Take a look at this Stack Overflow post
看看这篇 Stack Overflow 帖子
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.
我们已经实现了一个 JMX 方法来转储堆栈跟踪。这真的很方便,因为您甚至可以在远程机器上的 Web 浏览器中检查堆栈跟踪。