Thursday 24 November 2016

How Calculate the Size of java class Object by using premain method( Instrumention API)


How  Calculate the Size of java class Object by using premain method( Instrumention API)

In Java 1.5 new to API introduce that is  Instrumention API
Instrumention API  provides services needed to instrument Java programming language code. 

one use of  Instrumention API is use to get size of size of object in byte . 




Instrumention API   have an interface name  Instrumentation  .
 First of all we have to obtain an instance of the Instrumentation interface 

                         Step to get size of java class object 

               we create a special agent class with a special premain method, generally compiling it against the rest of our       code so that it can see the definitions of the classes whose memory usage we're interested in measuring;
  1. When a JVM is launched in a way that indicates an agent class. In that case an Instrumentation instance is passed to the premain method of the agent class.
    we package the compiled agent class into a jar with a special manifest file;

     we run our program, passing in the agent jar to the VM command line arguments.




    Step 1  First create the Agent class that is key to get size of object like following  .

     InstrumentAgent.java

    package in.jk;

    import java.lang.instrument.Instrumentation;

    public class InstrumentAgent {
       
         private static volatile Instrumentation globalInstrumentation; 
         
          
           public static void premain(final String agentArgs, final Instrumentation inst) 
           { 
              System.out.println("premain..."); 
              globalInstrumentation = inst; 
           } 
         
        public static long getObjectSize(final Object object) 
           { 
              if (globalInstrumentation == null) 
              { 
                 throw new IllegalStateException("Agent not initialized."); 
              } 
              return globalInstrumentation.getObjectSize(object); 
           } 
          
    }


    Sample java Object to calculate size of object 
    Employee.java
    package in.jk;

    public class Employee {
       
        private String empId;
        private String empName;
        private String desigination;
        private String salary;
        private String company;
       
       

       
       
       
        public String getEmpId() {
            return empId;
        }



        public void setEmpId(String empId) {
            this.empId = empId;
        }



        public String getEmpName() {
            return empName;
        }



        public void setEmpName(String empName) {
            this.empName = empName;
        }



        public String getDesigination() {
            return desigination;
        }



        public void setDesigination(String desigination) {
            this.desigination = desigination;
        }



        public String getSalary() {
            return salary;
        }



        public void setSalary(String salary) {
            this.salary = salary;
        }



        public String getCompany() {
            return company;
        }



        public void setCompany(String company) {
            this.company = company;
        }
    }


    Main class to run program  

    AgentMain.java
     
    package in.jk;

    public class AgentMain {
       

        public static void getObjectSize(final Object obj){
          
            System.out.println("Size Of Employee Object is  == "+ InstrumentAgent.getObjectSize(obj));
        }
          

       public static void main(String[] args) {
          

            Employee emp = new Employee();
            emp.setEmpId("VEL/2015");
            emp.setEmpName("J K Saini");
            emp.setDesigination("Java/J2EE Developer");
            emp.setSalary("20000");
            emp.setCompany("Velocis System pvt Ltd");
          
            AgentMain.getObjectSize(emp);
          
        }




    create a special manifest file with manifest.txt name .
    provide enrty for premain Agent class like as:

    Premain-Class : in.jk.InstrumentAgent



    After the write all above four file open command line prompt compile the all thrre java class by following command :

    javac  -d . *.java  

    it will  compile all of three java class inside package 

    then create jar file by adding manifest.txt file by following command 

    jar -cmf manifest.txt  jks.jar   in/jk/*.class

    above command create a packge jar with jks.jar

    then execute the following command to exeute Agent main class

    java  -javaagent:jks.jar  -cp . in.jk.AgentMain

    on command line you will get following out put