Object Serialization Pitfall
by, 04-25-2012 at 10:16 PM (533 Views)
It shall be kept in mind while dealing with object serialization that hashtable is maintained by the ObjectOutputStream which maps the objects which have been written in stream, to a handle. When object has been written for very first time to the stream, all of its contents are copied into stream. However, writes after it would be resulting in a handle, to that object which has been written to stream. Couple of problem might arise.
- When a written object to stream is written and modified again, all the notifications will be noted down while stream deserialization. Reason behind this is that it would be resulting into handle but the modified objects would not get copied to the stream. ObjectOutputStream.reset method shall be called to resolve this problem which will discard the memory to send the object; hence other writes would be copied to stream.
- When a lot of objects have been written into ObjectOutputStream, one might throw OutOfMemoryError. Reason is that references are maintained by the hashtable which is not reachable otherwise by application. Call ObjectOutputStream.reset method to solve this problem by resetting the handle or object table to the initial phase/state. When this method is called, all written objects become garbage collection eligible.