Monday, 7 October 2019

How to analyse heap dump (.hprof) file?


There are multiple tools available to analyse java heap dump. In this post, I am going to explain how to analyse heap dump using jhat, eclipse MAT (Memory Analyzer Tool).

Generate heap dump Programmatically
Step 1: Get the hotspot diagnostic MBean from the platform MBean server.

MBeanServer server = ManagementFactory.getPlatformMBeanServer();
HotSpotDiagnosticMXBean hotspotMBean = ManagementFactory.newPlatformMXBeanProxy(server, "com.sun.management:type=HotSpotDiagnostic", HotSpotDiagnosticMXBean.class);

Step 2: Use dumpHeap method of HotSpotDiagnosticMXBean to generate heap dump.

void dumpHeap(String outputFile, boolean live)
Dumps the heap to the outputFile file in the same format as the hprof heap dump. If the parameter 'live' is set to then this method dump only live objects i.e. objects that are reachable from others

Example
hotspotMBean.dumpHeap(fileName, true);

Find the below working application.

HeapDumpUtil.java
package com.sample.app.util;

import java.io.IOException;
import java.lang.management.ManagementFactory;

import javax.management.MBeanServer;

import com.sun.management.HotSpotDiagnosticMXBean;

public class HeapDumpUtil {
 private static final String HOTSPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic";

 private static HotSpotDiagnosticMXBean hotspotMBean = null;

 /* Get the hotspot diagnostic MBean from the platform MBean server */
 private static HotSpotDiagnosticMXBean getHotspotMBean() throws IOException {
  MBeanServer server = ManagementFactory.getPlatformMBeanServer();
  return ManagementFactory.newPlatformMXBeanProxy(server, HOTSPOT_BEAN_NAME, HotSpotDiagnosticMXBean.class);

 }

 public static void generateHeapDump(String fileName) throws IOException {
  if (hotspotMBean == null) {
   synchronized (HeapDumpUtil.class) {
    if (hotspotMBean == null) {
     hotspotMBean = getHotspotMBean();
    }
   }
  }

  hotspotMBean.dumpHeap(fileName, true);
 }

}


App.java
package com.sample.app;

import java.io.IOException;

import com.sample.app.util.HeapDumpUtil;

public class App {

 public static void main(String args[]) throws IOException  {
  HeapDumpUtil.generateHeapDump("dump1.hprof");
 }
}

Run App.java, you can see that dump file is generated with name dump1.hprof.

Use jhat to analyze heap dump
Execute below command to analyze heap dump.


jhat -port 7401 -J-Xmx4G dump1.hprof
$jhat -port 7401 -J-Xmx4G dump1.hprof
Reading from dump1.hprof...
Dump file created Mon Oct 07 08:56:44 IST 2019
Snapshot read, resolving...
Resolving 18355 objects...
Chasing references, expect 3 dots...
Eliminating duplicate references...
Snapshot resolved.
Started HTTP server on port 7401
Server is ready.

One the server is ready, open the url ‘http://localhost:7401/’, you can see all the heap information.

Using Eclipse MAT
You can go through my below post.
https://self-learning-java-tutorial.blogspot.com/2015/01/eclipse-memory-analyzer-tool-mat.html



Previous                                                    Next                                                    Home

No comments:

Post a Comment