Sizeof for Java

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.

Comments:

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 :-)

Posted by jjmahe on September 04, 2004 at 03:45 PM IST #

Post a Comment:
Comments are closed for this entry.
About

sundararajan

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
Bookmarks
Links

No bookmarks in folder

Blogroll

No bookmarks in folder

News

No bookmarks in folder