display | more...

In Java, the process which converts an object into a stream of bytes.

The classes ObjectInputStream and ObjectOutputStream provide methods to read/write objects from/to a simple stream (e.g. a file). The state of an object can thus be saved easily, and later restored. The object must be marked as serializable, by implementing the Serializable interface.

There's a pretty devilish memory leak hidden in Java's serialization process that will affect you if you have any such streams that are long-lived (like a permanent network connection or a log file). In order to save space or bandwidth, the ObjectOutputStream keeps references to all objects that have been sent through it. If the same object is sent again, only a short reference to the previous instance is sent, instead of the whole object.

Of course this means that no object ever sent through the stream can be reclaimed by the garbage collection until the stream is closed - a classic memory leak. You can fix it by periodically calling the reset method of the stream, which will remove the references. But if you don't know this, good luck finding the reason for the leak...

Serialization of a method means that calls to the method are handled one at a time. A serialized method cannot have more then one thread operating on it at a time.

Serialization is the enemy of scalability. A serialized method is like a grocery store with only one register open. If there are six people waiting to check out, then it will take close to six times the amount of time for the last one to finish then it would if six registers were open.

However, serialization does have its place. Java's Synchronization offers a way to serialize threads' access to a method.

Log in or register to write something here or to contact authors.