Ways to generate a Heap Dump

Java Troubleshooting
Sebastian Misiewicz
Java - Troubleshooting #2

Generate automatically on an incident


Add those JVM properties:
  • -XX:+HeapDumpOnCtrlBreak
  • -XX:+HeapDumpOnOutOfMemoryError
  • -XX:HeapDumpPath=c:\temp\heapdump\

Generate manually


jmap

jmap can be found under bin folder of your JDK

Starting from command line

First find out what is the PID of the java process, from which the dump will be generated. Then run from command line:

jmap -dump:file=c:\temp\heapdump\dump.bin PID

Starting from application

String name = ManagementFactory.getRuntimeMXBean().getName();
String pid = name.substring(0, name.indexOf("@"));
String[] cmd = {"jmap", "-dump:file=c:\\temp\\heapdump\\dump.bin", pid};
Process p = Runtime.getRuntime().exec(cmd);

HotSpotDiagnosticMXBean

ManagementFactory.getDiagnosticMXBean().dumpHeap("c:\\temp\\heapdump\\dump.bin", true);

jconsole

jconsole can be found under bin folder of your JDK

  1. Run jconsole
  2. Connect to your process via local process or remote connection
  3. Go to MBeans tab
  4. Find com.sun.management > HotSpotDiagnostic > dumpHeap
  5. As first parameter set "c:\\temp\\heapdump\\dump.bin"
  6. Execute