performance issue with serialization ?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Jean Bon (jean.bon_at_free.fr)
Date: 03/19/05


Date: Sat, 19 Mar 2005 23:51:44 +0100

hello
I have an application which manages a tree of objects. each object
in the tree have a lot of references on other objects in the tree.

in the application, I serialize a lot this tree using a binary formatter.
there are a lot of classes in the application, I simplified the problem
with those two classes A and B :

[Serializable]
class A : ISerializable
{
  private string s1, s2;
  protected A(SerializationInfo info, StreamingContext ctx)
  {
    s1= info.GetString("s1");
    s2= info.GetString("s2");
  }
  protected void GetObjectData(SerializationInfo info, StreamingContext ctx)
  {
    info.AddValue("s1", s1);
    info.AddValue("s2", s2);
  }
}

[Serializable]
class B : ISerializable
{
  private int i1, i2;
  private A parent;
  protected B(SerializationInfo info, StreamingContext ctx)
  {
    i1= (int)info.GetInt32("is1");
    i2= info.GetInt32("i2");
    parent = (A) GetValue("parent", typeof(A));
  }
  protected void GetObjectData(SerializationInfo info, StreamingContext ctx)
  {
    info.AddValue("i1", i1);
    info.AddValue("i2", i2);
    info.AddValue("parent", parent);
  }
}

if *a* is an instance of A and *a* is the root of the tree, I saw in the
debugger that when serializing this *a*, I go two times in GetObjectData
of A, one time when serializing *a* itself and another time when
serializing its reference in B. In my real application, GetObjectData
of *a* may be called more than 100 times, *a* is referenced by many
other objects...

questions :

-why so many calls to GetObjectData ? is it a bug in my application with
the way I implemented custom serialization ?
-I think I could not add *parent* in *info* when serializing and re-create
this reference in an OnDeserialization method that A would implement ?

something like that :

class A : ISerializable, IDeserializationCallback
{
 //...
  public void OnDeserialization(object sender)
  {
    son.SetParent(this);
  }
}

what do you think of this kind of solution to avoid multiple calls to
GetObjectData because of object circular references ?

thanks for all



Relevant Pages

  • Re: Tracking a memory leak.
    ... If you have a tree where the nodes are only referenced by the parent, ... and then the reference to the root is let go, ... and then what references they hold, and so on, and so on. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Tracking a memory leak.
    ... If you have a tree where the nodes are only referenced by the parent, ... and then the reference to the root is let go, ... Basically, the CLR performs a mark and sweep, where it starts with the ... and then what references they hold, and so on, and so on. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Tracking a memory leak.
    ... He correctly stated that the initial sweep doesn't start in the tree ... It starts on the _stack_ (and in other places mentioned by ... It then proceeds to follow all references ... IF the root of the tree is referenced by a "live" object (that has ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How do I find all references to an object in the JVM?
    ... >>references that are reachable via the tree you walk. ... >>current framework and object model. ...
    (comp.lang.java.programmer)
  • Re: validating AJAX
    ... on the browser in-memory DOM tree rather than on serialized markup, ... it for errors than serializing the tree and parsing the serialized form ...
    (comp.lang.javascript)