Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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));
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
–
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.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.