Is there any significant performance gain in serialization from declaring instance variables as transient?

Tim Rohaly

Serialization saves only the state of the object. If some instance variables don't contribute to the state, there's no sense in spending the extra time and bytes serializing them. As for how much performance you'll gain, that depends on your objects. If the instance variables are primitives, you won't save much, but if the instance variables are object references, the savings can be much greater.

If however the instance variable you declare transient are part of the state of the object, you will need to reconstruct the transient values upon deserializing. This requires you to implement private void readObject() and private void writeObject(). In general, objects which implement readObject() and writeObject() have better serialization performance because the object streams don't have to use reflection as much to figure out the detailed structure of the object. But this is by no means a hard-and-fast rule - it's easy to decrease performance by an inefficient readObject() or writeObject() method.

The only sure way to find out is to test it with your own objects, within your own application. Trying to "tune" the performance of your application by setting variables to be transient is probably not a good thing to do - it would be much better to design your objects for serialization from the start by understanding, on an object-by-object basis, what information needs to be serialized and making sure that variables which don't constitute the state are declared transient.

Further information about improving performance of serialization can be found at:

http://www.jguru.com/jguru/faq/view.jsp?EID=3419
and
http://www.jguru.com/jguru/faq/view.jsp?EID=42504
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact