如何在没有垃圾收集的情况下在 Java 5 上进行堆转储?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1268336/
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
How can I take a heap dump on Java 5 without garbage collecting first?
提问by Colin
We have a long running server application running Java 5, and profiling it we can see the old generation growing slowly over time. It's correctly freed on a full GC, but I'd like to be able to look at the unreachable objects in Eclipse MAT using a heap dump. I've successfully obtained a heap dump using +XX:HeapDumpOnCtrlBreak, but the JVM always does a GC before dumping the heap. Apparently this doesn't happen on Java 6 but we're stuck on 5 for now. Is there any way to prevent this?
我们有一个运行 Java 5 的长时间运行的服务器应用程序,分析它我们可以看到老年代随着时间的推移缓慢增长。它在完整的 GC 上被正确释放,但我希望能够使用堆转储查看 Eclipse MAT 中无法访问的对象。我已经使用 +XX:HeapDumpOnCtrlBreak 成功获得了堆转储,但 JVM 总是在转储堆之前执行 GC。显然这不会发生在 Java 6 上,但我们现在停留在 5 上。有什么办法可以防止这种情况吗?
采纳答案by skaffman
回答by daggett
use jconsole or visualvm or jmc or ... other jmx management console. open HotSpotDiagnostic in com.sun.management. select method dumpHeap and input two parameters:
使用 jconsole 或 visualvm 或 jmc 或...其他 jmx 管理控制台。在 com.sun.management 中打开 HotSpotDiagnostic。选择方法dumpHeap并输入两个参数:
- path to the dump file
- (true/false) dump only live objects. use
falseto dump all objects.
- 转储文件的路径
- (true/false) 仅转储活动对象。用于
false转储所有对象。
Note that the dump file will be written by the JVM you connected to, not by JVisualVM, so if the JVM is running on a different system, it will be written on that system.
请注意,转储文件将由您连接的 JVM 写入,而不是由 JVisualVM 写入,因此如果 JVM 运行在不同的系统上,它将写入该系统。
回答by Robert H?glund
Have you tried the standard jmap tool shipped with the JDK? The jmap toll was officially introduced in Java 5.
您是否尝试过 JDK 附带的标准 jmap 工具?jmap 收费是在 Java 5 中正式引入的。
Example command line: /java/bin/jmap -heap:format=b
示例命令行:/java/bin/jmap -heap:format=b
The result can be processed with the standard jhat tool or with GUI applications such as MAT.
结果可以使用标准的 jhat 工具或 GUI 应用程序(如 MAT)进行处理。
回答by Peter Thomas
I have some code here that can programmatically take a heap dump over JMX:
我这里有一些代码可以通过 JMX 以编程方式进行堆转储:
Link: JmxHeapDumper.java
The comments in the source code contain 2 links to articles that contained useful information about how to take heap dumps. I don't know for sure but if you are in luck, perhaps the JMX approach would have some way of avoiding the GC. Hope this helps !
源代码中的注释包含 2 个指向文章的链接,这些文章包含有关如何进行堆转储的有用信息。我不确定,但如果你很幸运,也许 JMX 方法可以通过某种方式避免 GC。希望这可以帮助 !
回答by Lawrence Dol
jProfiler (ej-technologies) can do this.
jProfiler ( ej-technologies) 可以做到这一点。


