Heap Dump & Analysis

What is a Heap dump & Analysis: A heap dump is a snapshot of the memory of a Java™ process. The snapshot contains information about the Java objects and classes in the heap at the moment the snapshot is triggered. Because there are different formats for persisting this data, there might be some differences in the information provided. Typically, a full garbage collection is triggered before the heap dump is written, so the dump contains information about the remaining objects in the heap.

The Memory Analyzer works with HPROF binary heap dumps, IBM system dumps, and IBM portable heap dumps (PHD) from various platforms. Typical information in a heap dump, depending on the heap dump type, includes: All ObjectsClass, fields, primitive values, and references. All ClassesClass loader, name, super class, and static fields. Garbage collection roots Objects defined to be reachable by the JVM. Thread Stacks and Local VariablesCall-stacks of threads at the moment of the snapshot, and information about local objects on a frame by frame basis.

How to take a Heap dump of your running Java Application

There are several ways to take a heap dump. We will talk about the 3 easiest ways to do it.

Command-line interface to generate the Heap Dump

These steps are common for all operating systems including Windows, Linux, and macOS.

  1. Find the process id of your running Java application. You can use the jps tool to list down all the running Java processes on your local machine. The processes will be listed in the following format “<pid> <MainClass>”
  2. After finding the pid, run the following command 
  3. jmap -dump:live,file=<file-name + .hprof> <pid>

The option live is important if you want to collect only the live objects i.e objects that still have a reference in the running code.

VisualVM to generate the Heap Dump

Visual VM makes it very easy to take a heap dump running on your local machine. The following steps can be used to generate heap dump using VisualVM

  1. Start Visual VM and connect your local Java Application to it.
  2. Under the Monitor Tab,  click on Heap Dump.
  3. After clicking on the heap dump you will be redirected to a new tab from which you can find out the location of your heap dump.
Heap dump & Analysis

JConsole to generate the Heap dump

  1. Connect your application to JConsole.
  2. Switch to MBeans tab and select com.sun.management > HotSpotDiagnostic > Operations > dumpHeap.
  3. Before clicking on the dumpHeap function, set the parameters p0, p1 described below.
    1. The parameter p0 is the location and the name of the heap dump file. Ensure that you add the “.hprof” extension at the end of the file name.
    2. The parameter p1, if set to true, performs a GC before dumping the heap so that only live objects are present in the heap dump.
Heap dump & Analysis

Pages: 1 2

Leave a Reply

Your email address will not be published. Required fields are marked *