添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
另类的麦片  ·  pymysql execute - CSDN文库·  7 月前    · 
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

That ObjectSizeFetcher requires that it be run as an agent. See docs.oracle.com/javase/6/docs/api/java/lang/instrument/… for details. – SamYonnou Jan 22, 2014 at 14:52

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.