Sunday, April 12, 2015

Java Heap Dump Analysis

Dumping heap on to a file:

package com;

import java.lang.management.ManagementFactory;

class A {
int i = 10;
}

public class Test {
static public void main(String[] args) throws Exception {

for(int i=0; i<10000; i++) {
new A();
}

String name = ManagementFactory.getRuntimeMXBean().getName();
String pid = name.substring(0, name.indexOf("@"));

String[] cmd = { "jmap", "-dump:file=/home/suresh/temp/dump.hprof",
pid };
Process p = Runtime.getRuntime().exec(cmd);

p.waitFor();

System.out.println("Completed...");
}
}

Analysing heap dump using Eclipse Memory Analyser Tool:
Install Memory Analyser tool in Eclipse, by searching in the installed eclipse site.

Then open the above saved .hprof file.