Cameron claims that an
Integer takes 81 bytes in the serialized form. A quick
experiment reveals that this is *not* true in general--the "limit" overhead for
serializing an Integer is 10 bytes. Since Cameron probably arrived at the figure 81 somehow, what is going
on here?
The issue is data versus metadata. Java serialization carries
not only instance data, but also metadata about the classes being serialized. This enables interesting
behavior such as recovery by the receiver even if classes have
changed since the data was serialized. It also makes the serialized
form fatter,
but this bloat is
per class, not per instance. So the first time you
serialize an Integer takes well more than ten bytes. After that, it's
ten.
Of course, ten bytes isn't so great either, since the
Integer itself is only four bytes long. For classes in
general, there are good reasons for this overhead, but the reasons don't
apply very well to Integer. So just say no. You shouldn't be serializing
Integer anyway--use primitive types on the wire.
My book (
which is free in PDF form) talks about these issues in detail.
11:05:13 PM
|
|