Translate

Sunday, February 23, 2014

Java Serialization behind the scenes

Serialization is the process of saving an object's state to a sequence of bytes; deserialization is the process of rebuilding those bytes into an object.
Why is serialization required?
Java components use serialization to transfer objects, for communication b/w Java components across the network.

Java's serialization algorithm

Let's say, you have a class with two byte variables which makes it size to 2bytes. However, size of serialized object is higher, for ex: it could be 51bytes.  How did it happen?

  • Serialization algo writes out the metadata of the class associated with an instance.
  • It recursively writes out description of the super-class until it finds java.lang.object. 
  • After metadata, it starts writing the actual data associated with the instance. However, this time, it starts from the top most super-class to the most-derived class.
An example explained in detail at http://www.javaworld.com/article/2072752/the-java-serialization-algorithm-revealed.html. Don't forget to go through this. 

No comments:

Post a Comment