Sundararajan's Weblog

  • Java
    September 3, 2004

Sizeof for Java

Guest Author

sizeof operator is not available in Java. But, at times you may want to know size of your Java objects. Size of an object depends on VM implementation. Number of header words in an object ( "class pointer" etc.), pointer size (32/64 bit) and alignment variations make object size dependent on VM implementation. An article by Vladimir Roubtsov explains how to measure sizeof objects.

With J2SE 1.5, we can get size in 2 new ways (as I know), without using profilers or native agents.

  1. We can use java.lang.instrument.Instrument.getObjectSize API.
  2. We can use jmap -histo to get object histogram. For each class, jmap prints number of objects and total size in bytes occupied by those objects. We can divide total size by number of objects to get size of an individual object.

Join the discussion

Comments ( 1 )
  • jjmahe Saturday, September 4, 2004
    A kind of SizeOf is such a recall of C :-)
    Isn't that the contrary of OO programming ? get
    rid of pointers isn't the achievement of Java ?
    For me it's like you need that when you're landing on a given platform and you need to optimize your storing factor.
    and for the curious, of course :-)
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.