To make it more clear, this is a quick example:

class A implements Serializable { public B b; }
class B implements Serializable { public A a; }
A a = new A();
B b = new B();
a.b = b;
b.a = a;

So what happens if we serialize a and b objects into a file and deserialize from that file?

I thought we get 4 objects, 2 of each. Identical objects but different instances.

But I'm not sure if there's anything else or is it right or wrong.

If any technology needed to answer, please think based on Java.

Thank you.


Java keeps track of the objects that have been written to the stream, and subsequent instances are written as an ID, not an actual serialized object.

So, for your example, if you write instance "a" to the stream, the stream gives that object a unique ID (let's say "1"). As part of the serialization of "a", you have to serialize "b", and the stream gives it another id ("2"). If you then write "b" to the stream, the only thing that is written is the ID, not the actual object.

The input stream does the same thing in reverse: for each object that it reads from the stream, it assigns an ID number using the same algorithm as the output stream, and that ID number references the object instance in a map. When it sees an object that was serialized using an ID, it retrieves the original instance from the map.

This is how the API docs describe it:

Multiple references to a single object are encoded using a reference sharing mechanism so that graphs of objects can be restored to the same shape as when the original was written

This behavior can cause problems: because the stream holds a hard reference to each object (so that it knows when to substitute the ID), you can run out of memory if you write a lot of transient objects to the stream. You solve that by calling reset().

  • So it will make it return to the original state after deserialization, right? As it is before the serialization with references to each other? – Seregwethrin May 31 '12 at 20:43
  • 1
    Yes, you end up with only 2 objects, referencing each other. – hectorct May 31 '12 at 20:50
  • 1
    I can't believe Java handles this automatically. I'm impressed – Cruncher Dec 7 '15 at 18:43

In Java this is solved by caching the serialized objects and writing the handle of it when it is written again.

See step 5 in http://docs.oracle.com/javase/6/docs/platform/serialization/spec/output.html.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.