Tuesday, 12 June 2012

Debugging 'Out of Memory' in Java

This document takes one through an example code deliberately executed in order to obtain the “out Of Memory” Exception. The below is the sequence

  1. Execute the program using the JVM option HeapDumpOnOutOfMemoryError. This ensures the JVM to dump data in file something like java_pidpid.hprof  in Java working directory 
  2. Execute jhat <data file name>
  3. The above step will start a local server, accessible via web browsers. This step will read the data file obtained from step #1 and then analyze the information, resolves the references and be ready for the further steps
  4. Open the IE (or Mozilla) with the below default URL
  1. The IE will list out all the references. This has the capability of drilling down from one wrapper object (or container object) to the contained objects. Example: On the landing page:
    1. Click on class WrapperClass”. This will show all the reference to the objects held by WrapperClass instance. Similarly, the step can be repeated to drill down on the references held to reveal the references held by the clicked reference. This is a top-down drilling.
    2. Click on “Show instance counts for all classes (including platform)This will show ALL the instances that were held at the moment the system crashed
                                          i.    Our example deliberately pushes Integer objects onto an arraylist contained as a member variable inside the WrapperClass.
                                         ii.    We should see a large number of instances of Integers (because, we have deliberately did that). Click on the “instances of Integer”.
                                        iii.    This should reveal ALL individual Integer Object instances
                                        iv.    Click on any of the object instance shown
                                         v.    That should show the “references to this object
                                        vi.    Repeat the step until the container object, WrapperClass, in our case is revealed 
                          This is the bottom-up drilling 

                   Hence, jhat comes extremely handy while debugging above scenarios.

         Main Program

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class MemorCrashTest {

                public static void main(String args[])
                                                WrapperClass w=new WrapperClass();
                                                File f=new File("D:\\Workflow.zip");
                                                try {
                                                                FileInputStream fis=new FileInputStream(f);
                                                } catch (FileNotFoundException e) {
                                                                // TODO Auto-generated catch block
                                                }catch (IOException e) {
                                                                // TODO Auto-generated catch block

WrapperClass.java -> This contains the ArrayList

import java.util.ArrayList;

public class WrapperClass {

                public ArrayList a =new ArrayList();

Compile and execute using the JVM parameters HeapDumpOnOutOfMemoryError

The program has crashed with “Out of Memory” Error. And, has produced the data file in c:\tmp\java_pid4072.hprof

Run jhat and pass the generated profile data for analysis

 Jhat has now analyzed the data and is ready for reports. Open the browser for further steps

  Click on the WrapperClass Instance

Bottom Up Drilldown – From Landing Page

Now you can dig into any level of classes to find the memory leakage.


No comments:

Post a Comment