How to take a heap dump with Eclipse OpenJ9?

Viewed 1850

With Oracle's Hotspot JVM, it looks like jmap -dump:file=/tmp/dump.txt <pid> can be used to take a heap dump.

However, Eclipse OpenJ9 doesn't include the jmap tool - and if you try to use the regular jmap with OpenJ9's jvm, it gives the exception:

Exception in thread "main" java.lang.ClassCastException: com.ibm.tools.attach.attacher.OpenJ9VirtualMachine incompatible with sun.tools.attach.HotSpotVirtualMachine
    at java.lang.ClassCastException.<init>(java.base@10.0.2-adoptopenjdk/ClassCastException.java:71)
    at sun.tools.jmap.JMap.executeCommandForPid(jdk.jcmd@10.0.2-adoptopenjdk/JMap.java:128)
    at sun.tools.jmap.JMap.dump(jdk.jcmd@10.0.2-adoptopenjdk/JMap.java:192)
    at sun.tools.jmap.JMap.main(jdk.jcmd@10.0.2-adoptopenjdk/JMap.java:110)

So, how can one take a heap dump with OpenJ9?

2 Answers

An OpenJ9 heap dump can be created with the command jcmd <PID> Dump.heap <path>.phd.

For example:

jcmd 1 Dump.heap /tmp/heap-dump.phd

Notice:

  • It must be run as the same user that the JVM is running as.
  • The PID must be the ID of the JVM process to be inspected. jps -l will list the available processes.

Alternatively, use YourKit to take a memory snapshot:

  • Download YourKit and extract it
  • Use the Console Attach Wizard e.g. bash ./YourKit-JavaProfiler-2021.3/bin/attach.sh
  • Capture a memory snapshot: java -jar ./YourKit-JavaProfiler-2021.3/lib/yjp-controller-api-redist.jar localhost 10001 capture-memory-snapshot

Sources:

You can use -Xdump:heap:events=user to enable heap dump when signal 3 is passed to OpenJ9 JVM. So, start you application with this option and then issue kill -3 <pid> to get the heap dump.

You can also use Xdump Option Builder tool for generating the -Xdump options based on your requirement.

Related