2 Minutes To Code (β Version)

Eclipse Memory Analyzer - 2 minute tutorial

Typical Code : Source code of classes whose heap dump will be analyzed in this tutorial

Given below is the source code of 2 classes StringStore and CalendarStore of our sample app whose jvm heap dump that will be analyzed in this tutorial.

StringStore has main(..) method and is that used to launch the sample app. It has a String field str which contains 1 million characters. It has reference calendarStore to an instance of CalendarStore.

The CalendarStore instance has a HashMap calMap which contains 1 million String-calendar key-value pairs.

After populating data, bith the classes (threads) call Thread.sleep(..) to allow us to get jvm heapdump.

Now run our program using the class StringStore. The next page shows how to get jvm heap dump using the command jmap.

Source code of StringStore.java

package dev.test;

public class StringStore
{
	static String str = "";
	public static void main(String[] args) throws Exception
	{
		CalendarStore calendarStore = new CalendarStore();
		calendarStore.start();
		for(int i = 0; i < 1000000; i++)
		{
			str += "a";
		}
		Thread.sleep(10000000);
	}
}



Source code of CalendarStore.java

package dev.test;

import java.util.Calendar;
import java.util.HashMap;


public class CalendarStore extends Thread
{
	static HashMap<String, Calendar> calMap = new HashMap<string, calendar="">();
	
	public void run()
    {
		for(int i = 0; i < 1000000; i++)
		{
			Calendar cal = Calendar.getInstance();
			calMap.put(cal.toString(), cal);
		}
		try { Thread.sleep(10000000); } catch (InterruptedException e) { e.printStackTrace(); }
	}
}