Monday, June 23, 2008

Size of objects in java

We all know that there is no operator or method to calculate size of an object in java. But can't really understand the reason for it, i mean if c++ can have it then why not java?
Only way to calculate appox. size of an objects might be to find out all the primitive data types forming the object and then add up their byte size.

If anybody knws a better a way or knws more size calculations then comments are welcome ...

2 comments:

PRK said...

Sizeof is handy in c/c++ since programmer has to manage (allocate/free) memory (scary).Java does not give this trouble. The another good reason is, even the size allocation differs from JVM to JVM. For performance improvement, a JVM might store a primitive type in different data size, so using sizeof, ofcourse, will give different results on different JVMs.

But there might be some cases when you actually need to know the size of an object, especially in profiling. Well, reflection could be the starting point.

Rakesh said...

i have found a good article on " How to find the Real Size of an Object in java"

http://tinyurl.com/5ggu5k