As an assignment question, I am asked to answer the following:

How are cycles handled? Where does the term graph come from?

In the examples given, there does not appear to be any clear trick that was done to serialize a cyclic linked list, so I can't discern if anything special is being done on the part of the programmer, or if it's handled internally by the JVM. Moreover, a Google search does not appear to answer the question other than to say that the JVM handles cyclic serialization natively by not re-serializing objects. But I'm not satisfied that this actually answers the question of "how".

My best guess is that some form of spanning tree algorithm is run on the object graph to remove cycles, but again, this is unconfirmed...

Where can I find information to help answer this question?

marked as duplicate by m3th0dman, ratchet freak, user40980, jwenting, GlenH7 Feb 13 '14 at 15:15

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


it caches the object and uses references so objects are not written out twice

5. If the object has already been written to the stream, its handle is written to the stream and writeObject returns.
12. For regular objects, the ObjectStreamClass for the class of the object is written by recursively calling writeObject. It will appear in the stream only the first time it is referenced. A handle is assigned for the object.


In other words it checks a IdentityHashMap<Object,Handle> for each object it writes out. and if present will just write out the handle.

this can be overridden by using writeUnshared(Object) to write out the object

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