Search code examples
c#limitsbinary-serialization

Is there any limits for binary serialization / deserialization object graph size in .net?


For example, i have:

struct SomeStruct
{
   //some fields
   //each instance will store info read from file, maybe be 3kb, maybe more.
}

List<SomeStruct> lst = new List<SomeStruct>();

I will add to that list crazy amount of objects, so it will end up to 10Gbs or more in size. Can i serialize lst without any errors like out of memory and etc? Can i deserialize it later?


Solution

  • If you can hold the list of items in memory at one time, you should have a decent chance of serializing/deserializing them. You may want to handle them individually, in a stream, rather than serializing/deserializing the entire list all at once, perhaps. That would take care of any edge cases you might have.

    Pseudo-code:

    private void SerializeObjects(List<foo> foos, Stream stream)
    {
        foreach (var f in foos)
        {
            stream.Write(f);
        }
    }
    
    private void DeserializeObjects(List<foo> foos, Stream stream)
    {
        foo f = stream.ReadFoo();
        while (f != null)
        {
            foos.Add(f);
            f = stream.ReadFoo();
        }
    }