Search code examples
javajvmheap-dumpheap-profiling

get a heap dump from a JRE only on Windows


Our product runs on our customer's servers with only a JRE(no JDK) on Windows.

I know that I can get a heap dump on OutOfMemoryError through a flag.

However, is there a way to get a heap dump from the JRE JVM process proactively ?

I was planning on getting a few healthy heap dumps and compare them with the heap dump obtained on OutOfMemoryError.

Preferably, I should be able to ask the customer to download this tool/utility and then run it on the server to obtain the heap dump at will.

I tried jvisualvm however jvisualvm would not list our product jVM. It would only list some of the JVMs running at that time and not all the JVMs running at that time. PLUS jvisualvm is very bulky.

P.S : Are there 3rd party utilities that can be executed on the host of JVM to dump the heap ? Also, customer cannot upgrade the JRE at this point of time. And none of the answers to the previous question are selected as answer!

Thank you,


Solution

    1. To dump heap from within JVM, use com.sun.management.HotSpotDiagnosticMXBean:

          HotSpotDiagnosticMXBean hs =
                  ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
          hs.dumpHeap("/path/to/heapdump.bin", false);
      
    2. To dump heap of another process from outside, use HotSpot Dynamic Attach mechanism.
      I wrote a tiny utility for that, see https://github.com/apangin/jattach.

      Build the program with make and then run

      jattach <PID> dumpheap /path/to/heapdump.bin