Java Serialization provides us a really very good useful in our developement. But, it has some disadvantages like : bad performance based on application and business requirements. The serialization depends on number and size of attributes, which want to serialize for an object.
There are following things for speeding up the marshaling and un-marshaling of objects during serialization:
Mark the unwanted or non Serializable attributes as transient.
Save only the state of the object, not the derived attributes. Some times we keep the derived attributes as part of the object however serializing them can be costly. Therefore consider calcualting them during de-serialization process.
Serialize attributes only with NON-default values.
Examples:- Serializing a int variable with value zero is just going to take extra space however, choosing not to serialize it would save you a lot of performance. This approach can avoid some types of attributes taking unwanted space. We will prefer to use Externalizable interface since attribute serialization is determined at runtime based on the value of each attribute.
To use Externalizable interface and implement the readExternal and writeExternal methods to dynamically identify the attributes to be serialized. We can write our custom logic used for serialization for various attributes.