0 Replies Latest reply on Jun 4, 2013 7:28 AM by Mrinmoy Biswas

    "Agent not initted" - using java.lang.instrument package

    Mrinmoy Biswas Newbie

      Hi,

      I am trying to find memory used by a class while the application runs. Earlier i tried Classmexer and i received "Agent not initted" errorstack. Now i have written a class with premain() method and using

      -javaagent:MyAgent-1.0.jar option in the VM arguments (MyAgent jar is created by me).

       

      Following is the MyAgent class code -

       

      package com.instrument;

      import java.lang.instrument.Instrumentation;

      public class MyAgent {

           private static volatile Instrumentation globalInstr;

          

           public static void premain(String args, Instrumentation inst) {

                System.out.println("************************* REACHED PREMAIN *************************");

                globalInstr = inst;

           }    

           public static long getObjectSize(Object obj) {

                if (globalInstr == null){

                          throw new IllegalStateException("Agent not initted");

           }    

           return globalInstr.getObjectSize(obj);

        }

      }

       

      Next i could see the text (REACHED PREMAIN) printed in the jboss console, which means jboss is reading MyAgent class. From my application i included MyAgent jar by maven and used MyAgent class in one class of my application as follows -

       

      long l = MyAgent.getObjectSize(this);

      LOGGER.debug("SIZE .. " + l);

       

      Still i am receiving "java.lang.IllegalStateException: Agent not initted" error. Actually i need to use classmexer for deepMemoryUsageOf() methods, but even this simple one is also not working. Any help would be appreciated.