Pages Navigation Menu

Coding is much easier than you think

Java Heap Dump Analysis using Eclipse Memory Analyzer (MAT)

Posted by on Mar 31, 2014 in Core Java, Eclipse, Java, JVM, Software | 1 comment

In this article we will learn what a Java heap dump is and how to analyze a heap dumps generated through OutOfMemoryError using Memory Analyzer in Eclipse.

What is a heap dump?

A heap dump is a snapshot of memory at a given point in time. It contains information on the Java objects and classes in memory at the time the snapshot was taken.

Why would we want to read heap dump?

If your Java application crashes with an OutOfMemoryError it is possible to automatically get a heap dump to analyze. This view into the memory profile of the application at the time it crashed can help us to figure out what caused the error. This can help decide what to optimize in our code.

How to get a heap dump?

To generate heap dump we have to execute a JVM with the following parameters in eclipse

-XX:+HeapDumpOnOutOfMemoryError writes heap dump on first  OutOfMemoryError 


Here the heap dump will be generated in the “current directory” of the JVM by default. It can be explicitly redirected with

-XX:HeapDumpPath= for example -XX: HeapDumpPath=/disk/myFolder. 

GC_Heap Dump analysis_Run_Configuration

GC_HeapDump anaylysis_Run_Configuration_JVM

How to read a heap dump?

Heap dump will be in binary format so you don’t read the plain file. Instead use a tool like Memory Analyzer Tool.
Download MAT plugin from this location, and install it in your eclipse.

OOM Java program


Here the Java program below is used to trigger an OutOfMemoryError. This program is basically creating multiple String instances within a List data structure until the Java Heap depletion.

package com.simplecode.heap;

import java.util.ArrayList;
import java.util.List;

public class OOMHeapGenerator 
public static void main(String[] args) 
	System.out.println("JVM OutOfMemoryError Simulator");
	List leakingVariable = new ArrayList();
		while (1 < 2) 
	catch (Throwable exp) 
	     if (exp instanceof java.lang.OutOfMemoryError) 
	     System.out.println("OutOfMemoryError triggered! " + "[" + exp + "]");
	     System.out.println("Other Exception! " + "[" + exp + "]");
	System.out.println("Simulator done!");


On running this program, when the JVM ran out of memory it created a heap dump file java_ pid1244.hprof.

GC_Heap Dump Analysis_Run_Java_Program_Exec

Press F5 in your project folder, so now the generated heap dump file will appear in eclipse.

Eclipse Heap Dump Memory Analyser

Load Heap Dump

Just double click on the heap dump file and select the Leak Suspects Report, and then this file will be loaded into MAT.

GC_Heap_Dump_analysis_Leak Suspect Report Using MAT
On clicking finish the following Screen is obtained


Analyze Heap Dump

Clicking on the “See stacktrace” link will give you the exact line of code that was building up the huge List causing the application to crash.

GC_Heap_Dump_analysis _Stacktrace
Now we know where to go look in at the code to fix this bug. Hence by analysing the Heap Dump using the MAT we could easily identify our primary leaking Java class and data structure.


Read More

What is a Memory Leak and Garbage Collector in java?

Posted by on Feb 11, 2014 in Java, JVM | 0 comments


A memory leak is the gradual loss of available memory when a program/application repeatedly fails to free memory that it has occupied for temporary use.
Memory Leak in java


Consider that you have 10 glasses at your house. Whenever these glasses are used they will be cleaned up and kept ready in the stand for future use by the servant when he comes. If you have placed one of the glasses in a room where the servant is not permitted to enter or you have not consumed the juice completely and it is still left in the glass, the servant might think that the glass is still in use and hence he will not clean them.
If this scenario continues, one day you will find all the glasses are uncleaned, so you call the servant to clean all of them.

In java this servant is known as Garbage Collector.

Read More

Catching out of memory error in java

Posted by on Feb 24, 2013 in Java, JVM | 1 comment

Catching out of memory error in java
Catching out of memory error in java
You can catch it as it descends from Throwable:

try {
    // create lots of objects here and stash them somewhere
} catch (OutOfMemoryError E) {
    // release some (all) of the above objects


Read More

Getting JVM heap size, used memory, total memory using Java Runtime

Posted by on Feb 23, 2013 in Java, JVM | 1 comment

Java’€™s Runtime class provide lot of information about the resource details of JVM(Java Virtual Machine). The memory consumed by the JVM can be read by different methods in Runtime class.
In the following example, at start of program we get initial size of heap of JVM by calling freeMemory, totalMemory and maxMemory and then we create thousands of object which occupy space in heap which forces JVM to extend heap, now call to total memory, free memory will return different value based on current heap size but max memory will still return same. Suppose, when the object created requires large amount of memory, then JVM will begin to throw OutOfMemoryError.

import java.util.ArrayList;
public class TestMemoryUtil
private static final int MegaBytes = 1024 * 1024;

public static void main(String args[]) 

long totalMemory = Runtime.getRuntime().totalMemory() / MegaBytes;
long maxMemory = Runtime.getRuntime().maxMemory() / MegaBytes;
long freeMemory = Runtime.getRuntime().freeMemory() / MegaBytes;

System.out.println("**** Heap utilization Analysis [MB] ****");
System.out.println("JVM totalMemory also equals to initial heap size of JVM :"+ totalMemory);
System.out.println("JVM maxMemory also equals to maximum heap size of JVM: "+ maxMemory);
System.out.println("JVM freeMemory: " + freeMemory);

ArrayList<String> objects = new ArrayList<String>();

 for (int i = 0; i < 10000000; i++) 
	objects.add(("" + 10 * 2710));

totalMemory = Runtime.getRuntime().totalMemory() / MegaBytes;
maxMemory = Runtime.getRuntime().maxMemory() / MegaBytes;
freeMemory = Runtime.getRuntime().freeMemory() / MegaBytes;

System.out.println("Used Memory in JVM: " + (maxMemory - freeMemory));
System.out.println("totalMemory in JVM shows current size of java heap:"+totalMemory);
System.out.println("maxMemory in JVM: " + maxMemory);
System.out.println("freeMemory in JVM: " + freeMemory);





**** Heap utilization Analysis [MB] ****
JVM totalMemory also equals to initial heap size of JVM : 15
JVM maxMemory also equals to maximum heap size of JVM: 247
JVM freeMemory: 15
Used Memory in JVM: 202
totalMemory in JVM shows current size of java heap : 118
maxMemory in JVM: 247
freeMemory in JVM: 45


lightbulb3 For printing the memory utilization in Kilobytes, just assign the value 1024 to the int variable MegaBytes.


Note : This is not the best way to know the sizes and in practice it will report less size that what have you specified in €“Xmx and €“Xms but still its working solution for most of needs.

Read More