This discussion is archived
5 Replies Latest reply: Jun 21, 2010 1:16 AM by EJP RSS

Some basic serialization questions

843790 Newbie
Currently Being Moderated
Hi everyone,

I'm quite new to serialization. I read some online introductions and got the very basics, but I have a couple of questions (which are still quite basic).

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? What is the correct and efficient way of achieving what I want?

2. Is it possible to visualize the serialized object, that is, dump it to some human-readable format (something like Data::Dumper in perl)?

Thank you,
David
  • 1. Re: Some basic serialization questions
    EJP Guru
    Currently Being Moderated
    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.
  • 2. Re: Some basic serialization questions
    843790 Newbie
    Currently Being Moderated
    Thank you!
  • 3. Re: Some basic serialization questions
    EJP Guru
    Currently Being Moderated
    In general, object graphs are presented.
    Whoops! In general, object graphs are preserved.
  • 4. Re: Some basic serialization questions
    843790 Newbie
    Currently Being Moderated
    Hi again,

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

    Thanks!
  • 5. Re: Some basic serialization questions
    EJP Guru
    Currently Being Moderated
    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.