This content has been marked as final. Show 5 replies
1. I have a few instances of the same class X. One of the fields of the class is a reference to an instance of another class Y. All X instances point to the same instance of Y at run time. I want to dump them all, and maintain that relation when later retrieving them. What happens if I dump each instance of X separately?The subsequent instances of X will all point to the Y instance that was deserialized along with the first X. In general, object graphs are preserved.
What is the correct and efficient way of achieving what I want?It works as you want by default.
2. Is it possible to visualize the serialized object, that is, dump it to some human-readable format (something like Data::Dumper in perl)?java.beans.XMLEncoder.
In general, object graphs are presented.Whoops! In general, object graphs are preserved.
I now encountered the notorious OutOfMemoryError when writing multiple objects to the stream. I read some stuff around and I understand it's due to the fact that ObjectOutputStream maintains a table mapping objects written into the stream to a handle. Increasing Xmx helped a little, but still, I'm think I'm pushing it and I wonder if there is a better solution.
Using ObjectOutputStream.reset() after each writeObject() is a bit problematic, since all my objects hold a reference to the same instance of another class, and resetting will make this other class be written 1000 times instead of once.
Is there some workaround? Again, the bottom line is I have 1000 object I would like to write, they are independent except one field in each object refereeing to the same class (and I want to maintain this, so that obj1.otherClassInstance == obj2.otherClassInstance after serializing).
If the 1000 objects themselves are distinct, use writeUnshared(). That will cause the immediate objects being written not to be shared, but whatever they refer to remains shared, as you want.