Thursday, 21 March 2013

Real To Java for fun


I had this weird observation yesterday thought of sharing with you:

I was on the way from my home station (Journal Square) to office (33d Street NY) in train  and as always I had taken the  corner most seat of the wagon and was busy reading some newspaper article . After couple of stops I realized a super hot blonde girl with the minimal of formal outfit you could think of  was sitting opposite to me . As most of us do , I too tried avoiding a stare towards her but again as always couldn’t avoid and somehow or the other kept on looking at her ( for some reason , there was this James blunt’s song , You’r beautiful  started playing in my head  )  . Anyways 23rd street arrived and again as always she left the train without even noticing me and the respect I had grown for her beauty J . So with heavy heart my stop arrived and I got down thinking of her and felt I LOVE BLONDE GIRLS.

Then as every day morning I proceeded towards a star bucks coffee shop next to my office to pick my latte ( as I have realized that if you want to be noticed in NY , you need to carry star bucks coffee even if you end up dumping it in your restroom, but somehow I like their latte). And as I entered I saw Christine again , a dark eyed ,dark haired beautiful Mexican girl, smiling and asked me do I need latte again ? ( as she suggests me many times to try some other coffee’s too at that place , but I don’t end up taking risk) . I picked my coffee , paid my bill and came out of the shop thinking I LOVE MEXICAN GIRLS.

At this moment , I realized what happened to my love for blonde girls ? Why did it change suddenly?

The first thing that came to my mind was , BLONDE and MEXICAN are 2 classes in my brains which are loaded and with every girl of that type and instance is created for them and if they are beautiful they are persisted else made available for garbage collection.  So when I saw the girl in train , my JVM loaded a  class called Blonde and quickly made an instance and filled all the variables in it J . Then when I went to coffee shop , it loaded a class called MEXICAN , again made a instance  of it with features of
Christine . But what happened to my class and object of blonde , it should have been still live. How does brain takes care of so many different classes of girls ?

The only reason I thought was if classes were getting unloaded back to main storage from my RAM . So does it even happen in JAVA . But I have never faced it in JAVA. neither I do take care of it while coding .

So I searched JVM doc to find the answer and I found it on a short note up which says A class or interface may be unloaded if and only if its defining class loader may be reclaimed by the garbage collector’

So if we write a custom class loader without much care and this gets unloaded so all the classes it loaded will get unloaded.

The dangers could be : If any class which was loaded before get unloaded ( due to classloader) but gets loaded again , all the static blocks would run again . And if you had some DB calls or Business logic present , this might be even more dangerous .

I understand it’s not a daily activity to write a classloader but thought of sharing with you if you some day end up finding a issue like this.


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
http://localhost:7000
  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[])
                {
                                for(;;)
                                {
                                                WrapperClass w=new WrapperClass();
                                                File f=new File("D:\\Workflow.zip");
                                                try {
                                                                FileInputStream fis=new FileInputStream(f);
                                                               
                                                                while(fis.read()!=-1)
                                                                {
                                                                                w.a.add(fis.read());
                                                                }
                                                                System.out.println(w.a);
                                                } catch (FileNotFoundException e) {
                                                                // TODO Auto-generated catch block
                                                                e.printStackTrace();
                                                }catch (IOException e) {
                                                                // TODO Auto-generated catch block
                                                                e.printStackTrace();
                                                }
                                }
                }
               
}


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.