Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 21 down vote accepted

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().

share|improve this answer
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
Yes, you end up with only 2 objects, referencing each other. – hectorct May 31 '12 at 20:50
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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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