简体   繁体   English

如何在没有垃圾收集的情况下在Java 5上进行堆转储?

[英]How can I take a heap dump on Java 5 without garbage collecting first?

We have a long running server application running Java 5, and profiling it we can see the old generation growing slowly over time. 我们有一个运行Java 5的长期运行的服务器应用程序,并对其进行分析,我们可以看到老一代随着时间的推移缓慢增长。 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. 它在完整的GC上正确释放,但我希望能够使用堆转储查看Eclipse MAT中无法访问的对象。 I've successfully obtained a heap dump using +XX:HeapDumpOnCtrlBreak, but the JVM always does a GC before dumping the heap. 我已成功使用+ XX:HeapDumpOnCtrlBreak获取堆转储,但JVM在转储堆之前始终执行GC。 Apparently this doesn't happen on Java 6 but we're stuck on 5 for now. 显然这不会发生在Java 6上,但我们现在仍然坚持使用5。 Is there any way to prevent this? 有什么方法可以防止这种情况吗?

use jconsole or visualvm or jmc or ... other jmx management console. 使用jconsole或visualvm或jmc或...其他jmx管理控制台。 open HotSpotDiagnostic in com.sun.management. 在com.sun.management中打开HotSpotDiagnostic。 select method dumpHeap and input two parameters: select方法dumpHeap并输入两个参数:

  • path to the dump file 转储文件的路径
  • (true/false) dump only live objects. (true / false)仅转储活动对象。 use false to dump all objects. 使用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在另一个系统上运行,它将写在该系统上。

在此输入图像描述

Have you tried the standard jmap tool shipped with the JDK? 您是否尝试过JDK附带的标准jmap工具? The jmap toll was officially introduced in Java 5. 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工具或MAT应用程序来处理。

I have some code here that can programmatically take a heap dump over JMX: 我在这里有一些代码可以通过编程方式在JMX上进行堆转储:

Link: JmxHeapDumper.java 链接: JmxHeapDumper.java

The comments in the source code contain 2 links to articles that contained useful information about how to take heap dumps. 源代码中的注释包含2个指向文章的链接,这些文章包含有关如何进行堆转储的有用信息。 I don't know for sure but if you are in luck, perhaps the JMX approach would have some way of avoiding the GC. 我不确定,但如果你运气好的话,也许JMX方法会有一些避免GC的方法。 Hope this helps ! 希望这可以帮助 !

I suggest a 3rd-party profiler such as YourKit , which may allow you to take snapshots without kicking off the GC first. 我建议使用第三方分析器,例如YourKit ,这可能允许您在不首先启动GC的情况下拍摄快照。 Added bonus, you can take a snapshot without the whole ctrl-break shenanigans. 添加奖金,你可以拍摄快照而不需要整个ctrl-break shenanigans。

jProfiler( ej-technologies )可以做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM