Re: Serizlize You Cannot Use for Object Size
On Aug 25, 1:50 am, molesky...@yahoo.com wrote:
One thread here said you can serialize object and count serialized
bytes to get the size of the object
This is incorrect. I serialize small class with one boolean and 2
chars and
got something crazy like 637 bytes for the size. I serialize to file
and you see the problem:
=BC=CF=86 =E2=99=A3{sr java.io.NotSerializableException(Vx =CF=84=C3=
=A5=E2=96=AC5=E2=98=BB xr
=E2=86=94java.io.ObjectStreamExce
ptiond=E2=94=9C=CE=A3k=C3=AC9=E2=88=9A=E2=96=80=E2=98=BB xr =E2=80=BCjav=
a.io.IOExceptionl=C3=87sde%=E2=89=A1 =BD=E2=98=BB xr =E2=80=BC
java.lang.Exception=E2=95=A8 =B2=E2=96=BC>
There is many bytes serialized so you can't use this to count size of
object in bytes.
First of all, what is meant by Object's size? This is not C/ C++. In
C, C++ the object size is calculated by summing up the member
variables. But, in java, it depends on implementation. For example,
the java language specification just says that the boolean should take
either 'true' or 'false'. But, it doesnt force any constraints on the
implementation like, the boolean size should be 1 or 10 bytes. Some
implementation might represent a boolean variable in 1 single byte.
Some other implementation may represent the boolean varibale in 2
bytes or more. So, size is all about implementation specific.
One more thing, the 'Object Construction' is also implementation
specific. Assume, you declare 5 integers in a serialized class. So,
you think that the object instance size for the class will be (5 * 4)
20 bytes. But, that cant be the case always. Because, jvm might add
some internal fields to represent the 'serialized' feature. Or it
might do some trick over constructing the particular instance. So, no
guarantee that your measured 'size' will be accurate.
I would suggest not to talk about *size* in java. Talk about memory.
--
Manivannan Palanichamy (@) Oracle.com
http://mani.gw.googlepages.com/index.html