开发者

There is no sizeof operator in java. How can I find size of an object in java? [duplicate]

开发者 https://www.devze.com 2022-12-18 03:14 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicates: Programatically calculate memory occupied by a Java Object including objects it references
This question already has answers here: Closed 12 years ago.

Possible Duplicates:

Programatically calculate memory occupied by a Java Object including objects it references

In Java, what is the best way to determine the s开发者_Python百科ize of an object?

Hi

There is no sizeof operator in java .. But how can i know the size of an object in java? Thanks


You may serialize the object to a byte array output stream.

That won't give you the exact size of the object because it will skip the transient data, but at least will give you a good idea of how big your objects are.


You can't. The internal representation is JVM-specific. If you want an empirical estimate for your JVM them you can try this: 1) turn on verbose garbage collector output (see JVM flags) 2) in your program invoke a full GC 3) create a lot of copies of the same object 4) invoke a full GC again 5) compare the heap size after steps 2 and 4 and divide by the number of objects


There is no portable direct way, and the answer is different between JVMs (since the spec is silent on implementation details).

Here's an excellent article showing one approach.


My first question would be what are you doing that requires sizeof? There are no pointers for you to manipulate, so you really have no need to know.

SO Duplicate?: In Java, what is the best way to determine the size of an object?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号