2

In reference to : Calculate size of Object in Java

I tried to run this program and get the size of the object created

  //main 
 while(true){
        Integer x=new Integer(50);
        System.out.println("HashCode of this object="+x.hashCode()+" 
                  ,  with Size of "+ObjectSizeFetcher.getObjectSize(x));
        i++;
}

 The output -- NullPointerException is thrown when getObjectSize() is invoked.

   new Integer() -->

should have created a new wrapper object. Instead it creates NPE. Why ?

java version "1.6.0_65" Apple OSX - 10.9.1 --Ignore Syntax errors

Community
  • 1
  • 1
user1428716
  • 2,078
  • 2
  • 18
  • 37

1 Answers1

1

Because the static field ObjectSizeFetcher.instrumentation was null when you called ObjectSizeFetcher.getObjectSize(x).

You should call method ObjectSizeFetcher.premain(String args, Instrumentation inst) before, with a non-null inst argument.

Bludzee
  • 2,733
  • 5
  • 38
  • 46