How can I take a heap dump on Java 5 without garba

2019-01-19 00:40发布

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?

5条回答
Anthone
2楼-- · 2019-01-19 01:19

use jconsole or visualvm or jmc or ... other jmx management console. open HotSpotDiagnostic in com.sun.management. select method dumpHeap and input two parameters:

  • path to the dump file
  • (true/false) dump only live objects. use false to dump all objects.

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.

enter image description here

查看更多
干净又极端
3楼-- · 2019-01-19 01:23

jProfiler (ej-technologies) can do this.

查看更多
你好瞎i
4楼-- · 2019-01-19 01:28

I suggest a 3rd-party profiler such as YourKit, which may allow you to take snapshots without kicking off the GC first. Added bonus, you can take a snapshot without the whole ctrl-break shenanigans.

查看更多
一夜七次
5楼-- · 2019-01-19 01:28

Have you tried the standard jmap tool shipped with the JDK? The jmap toll was officially introduced in Java 5.

Example command line: /java/bin/jmap -heap:format=b

The result can be processed with the standard jhat tool or with GUI applications such as MAT.

查看更多
Emotional °昔
6楼-- · 2019-01-19 01:31

I have some code here that can programmatically take a heap dump over 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 !

查看更多
登录 后发表回答