title: OutOfMemoryException when serializing large object tree
body: <p>I am serializing an object that has this structure: </p>
  
  <pre><code>[ProtoContract]
  public class Foo{
     // some more properties 
  
     [ProtoMember(100)]
     public IEnumerable&lt;Bar&gt;{get;set}
  }
  
  [ProtoContract]
  public class Bar{
     // a lot of properties
  }
  </code></pre>
  
  <p>I am using this code to write the protobuf:</p>
  
  <pre><code> var memoryStream = new MemoryStream();
   await new ProtoBufFormatter().WriteToStreamAsync(typeof(T), obj, ms, null, null);
  </code></pre>
  
  <p>As soon as the object tree gets over a certain size (10000> and more items) - I end up with an OutOfMemoryException. </p>
  
  <p>I am wondering if there are ways to tell protobuf-net to do a better serialization (to avoid the OutOfMemoryException) or if there are other ways to get around the problem? </p>
  
  <p><strong>Update</strong>
  I get the following exception: </p>
  
  <pre>
  Exception of type 'System.OutOfMemoryException' was thrown.
     at ProtoBuf.BufferPool.ResizeAndFlushLeft(Byte[]& buffer, Int32 toFitAtLeastBytes, Int32 copyFromIndex, Int32 copyBytes) in c:\Dev\protobuf-net\protobuf-net\BufferPool.cs:line 60
     at ProtoBuf.ProtoWriter.DemandSpace(Int32 required, ProtoWriter writer) in c:\Dev\protobuf-net\protobuf-net\ProtoWriter.cs:line 501
     at ProtoBuf.ProtoWriter.WriteString(String value, ProtoWriter writer) in c:\Dev\protobuf-net\protobuf-net\ProtoWriter.cs:line 600
     at proto_29(Object , ProtoWriter )
     at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Write(Object value, ProtoWriter dest) in c:\Dev\protobuf-net\protobuf-net\Serializers\CompiledSerializer.cs:line 53
     at ProtoBuf.Meta.RuntimeTypeModel.Serialize(Int32 key, Object value, ProtoWriter dest) in c:\Dev\protobuf-net\protobuf-net\Meta\RuntimeTypeModel.cs:line 752
     at ProtoBuf.ProtoWriter.WriteObject(Object value, Int32 key, ProtoWriter writer) in c:\Dev\protobuf-net\protobuf-net\ProtoWriter.cs:line 46
     at proto_27(Object , ProtoWriter )
     at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Write(Object value, ProtoWriter dest) in c:\Dev\protobuf-net\protobuf-net\Serializers\CompiledSerializer.cs:line 53
     at ProtoBuf.Meta.RuntimeTypeModel.Serialize(Int32 key, Object value, ProtoWriter dest) in c:\Dev\protobuf-net\protobuf-net\Meta\RuntimeTypeModel.cs:line 752
     at ProtoBuf.ProtoWriter.WriteObject(Object value, Int32 key, ProtoWriter writer) in c:\Dev\protobuf-net\protobuf-net\ProtoWriter.cs:line 46
     at proto_23(Object , ProtoWriter )
     at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Write(Object value, ProtoWriter dest) in c:\Dev\protobuf-net\protobuf-net\Serializers\CompiledSerializer.cs:line 53
     at ProtoBuf.Meta.RuntimeTypeModel.Serialize(Int32 key, Object value, ProtoWriter dest) in c:\Dev\protobuf-net\protobuf-net\Meta\RuntimeTypeModel.cs:line 752
     at ProtoBuf.ProtoWriter.WriteObject(Object value, Int32 key, ProtoWriter writer) in c:\Dev\protobuf-net\protobuf-net\ProtoWriter.cs:line 46
     at proto_9(Object , ProtoWriter )
     at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Write(Object value, ProtoWriter dest) in c:\Dev\protobuf-net\protobuf-net\Serializers\CompiledSerializer.cs:line 53
     at ProtoBuf.Meta.RuntimeTypeModel.Serialize(Int32 key, Object value, ProtoWriter dest) in c:\Dev\protobuf-net\protobuf-net\Meta\RuntimeTypeModel.cs:line 752
     at ProtoBuf.ProtoWriter.WriteObject(Object value, Int32 key, ProtoWriter writer) in c:\Dev\protobuf-net\protobuf-net\ProtoWriter.cs:line 46
     at proto_6(Object , ProtoWriter )
     at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Write(Object value, ProtoWriter dest) in c:\Dev\protobuf-net\protobuf-net\Serializers\CompiledSerializer.cs:line 53
     at ProtoBuf.Meta.RuntimeTypeModel.Serialize(Int32 key, Object value, ProtoWriter dest) in c:\Dev\protobuf-net\protobuf-net\Meta\RuntimeTypeModel.cs:line 752
     at ProtoBuf.ProtoWriter.WriteRecursionSafeObject(Object value, Int32 key, ProtoWriter writer) in c:\Dev\protobuf-net\protobuf-net\ProtoWriter.cs:line 75
     at proto_5(Object , ProtoWriter )
     at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Write(Object value, ProtoWriter dest) in c:\Dev\protobuf-net\protobuf-net\Serializers\CompiledSerializer.cs:line 53
     at ProtoBuf.Meta.RuntimeTypeModel.Serialize(Int32 key, Object value, ProtoWriter dest) in c:\Dev\protobuf-net\protobuf-net\Meta\RuntimeTypeModel.cs:line 752
     at ProtoBuf.Meta.TypeModel.SerializeCore(ProtoWriter writer, Object value) in c:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:line 186
     at ProtoBuf.Meta.TypeModel.Serialize(Stream dest, Object value, SerializationContext context) in c:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:line 217
     at WebApiContrib.Formatting.ProtoBufFormatter.WriteToStreamAsync(Type type, Object value, Stream stream, HttpContent content, TransportContext transportContext)
  </pre>