There is no sizeof operator in java. How can I find size of an object in java? [duplicate]
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?
精彩评论