Sunday, 7 December 2014

JAVA : HOW CAN WE SEE, HOW MANY OBJECTS ARE CREATED FOR CLASSES AT MEMORY LEVEL(AT HEAP LEVEL)

Standard
In this post we will see how can we see how many objects are created for classes at memory level. means that how many objects are created in heap we can see by using java visual machine this is a .exe file available in jdk ,with in bin folder.
see path : C:\Program Files\Java\jdk1.7.0_71\bin 

By double cliking on this .exe file we can open this tool so that we can analyze how many objects were created  in heap for classes, before going to see first of all let me create one java sample program so that we can see how many objects are created for that program. so let's go.


Program:-
------------
  1. package com.system;  
  2.   
  3.    
  4.  class Test{  
  5.     
  6.   public void m1(){  
  7.    System.out.println("We are in Test class");  
  8.   }  
  9.  }  
  10.    
  11.  class Team extends Test{  
  12.     
  13.   public void m1(){  
  14.    System.out.println("We are in Team class");  
  15.   }  
  16.     
  17.  }  
  18. public class TestMemoryLevel {  
  19.   
  20.  public static void main(String[] args) throws InterruptedException {  
  21.   // TODO Auto-generated method stub  
  22.   
  23.   Team t =new Team();  
  24.   t.m1();  
  25.   Test t1 =new Test();  
  26.   t1.m1();  
  27.   TestMemoryLevel t2=new TestMemoryLevel();  
  28.   Thread.sleep(10*500*40);  
  29.     
  30.  }  
  31.   
  32. }  

In the above program  you can observe that i have created three classes . In main class that is TestMemoryLevel i have created objects for  Three classes i.e for Test,Team,TestMemoryLevel from that you can say how many objects are created for these classes easily.Answer is 3 for each class we are creating one object.But our aim is to see is  really three objects are created for three classes or not in heap level i.e is in memory level.SO for proof let's go.In our program we are  calling sleep method of Thread class , so that our program will have some process ID , so that we can analyse easily for better understanding  i will explain step wise with screenshots.

1) compile and run the program.

2) Then go to C:\Program Files\Java\jdk1.7.0_71\bin folder you can see jvisualvm.exe double click on this.



3) After that you can observe that one process ID coming with package name see below. then double click on that.


4) After that you will see below window beside of above window. then click on "monitor"  button


5) After clicking on monitor button you will see some graphical charts.On the top of the right side corner you can see "Heap Dump" button


6) click on that Heap Dump button you will see below window , there you can see classesoption click on that. there you will see lot of classes . search with your  package name, you can see search option below of the window. Finally you will see how many objects were created with memory level proof  for your classes. That's it guys if you have any doubts please leave comment below i will clarify as soon as possible.




0 comments:

Post a Comment