6

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?

Ali
  • 261,656
  • 265
  • 575
  • 769
  • According to [this issue](https://github.com/eclipse/openj9/issues/5317) jmap should be part of the current/upcoming release. – Thomas Jul 01 '19 at 09:03
  • Thanks, what should I do this in the meantime? @Thomas – Ali Jul 01 '19 at 09:04
  • Hmm, I'm no OpenJ9 expert yet (we're currently discussing to switch) but it seems as if you could use Eclipse MAT to connect to the JVM and acquire a dump or use [-Xdump](https://www.eclipse.org/openj9/docs/xdump/) options to enable various dump agents (e.g. "heap") and events (e.g. "user"). – Thomas Jul 01 '19 at 09:08
  • @Thomas Yeah I tried reading the `-xdump` documentation and I think I'm an experienced dev but I can't figure out which options I'd need or how to use them. All I want is to take a dump before and after taking a particular action. – Ali Jul 01 '19 at 09:13
  • It looks like it only produces dumps for particular events. I want to trigger the dump myself. – Ali Jul 01 '19 at 09:14
  • Well, I didn't use those options either but according to the documentation the event type "user" should allow you to send an event to the process. Another option might be using [JMX](https://www.ibm.com/support/knowledgecenter/SSYKE2_8.0.0/com.ibm.java.vm.80.doc/docs/mxbeans.html), e.g. via jconsole. – Thomas Jul 01 '19 at 09:26

2 Answers2

2

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:

Fernando Correia
  • 21,803
  • 13
  • 83
  • 116
  • Just an additional tip. once you create the .phd dump, you can use the *Eclipse Memory Analyzer (MAT)* tool with *IBM Monitoring and Diagnostic Tools* plugin to see its contents, possible leaks, etc. See Analyzing Dumps sections [here](https://www.eclipse.org/openj9/docs/dump_heapdump/) – emrekgn Sep 09 '22 at 13:04
1

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.

Ashutosh
  • 181
  • 5