| Sizeof For Java(tm)
Java(tm) has no sizeof() operator like C/C++.With uniform sizes for primitive data types,
and a different style of memory allocation, the need for sizeof() really isn't there.
And it's hard to define what sizeof() would mean anyway, given that an object may not
contain other objects, but only references to them.
But it's interesting to experiment with the 1.1 reflection feature and see whether a
method can be devised that will return useful information about object sizes.
The Sizeof class below tries to do this, for a passed-in data structure. It walks the
structure and tallies up the total size in bytes. It ignores alignment and packing issues
and hidden fields in structures, and assumes a boolean is of size 1 and a reference of
size 4 (reference sizes may vary; for example SZ_REF might be 8 on a machine with 64-bit pointers).
It does not count static data members of class instances, but does include members
inherited/implemented from superclasses and interfaces. It does not follow references in
object instances or in arrays, except for the case of a multi-dimensional array, where the
reference is to another array.
|