OutOfMemoryException when serializing large object tree
I am serializing an object that has this structure:
[ProtoContract]
public class Foo{
// some more properties
[ProtoMember(100)]
public IEnumerable<Bar>{get;set}
}
[ProtoContract]
public class Bar{
// a lot of properties
}
I am using this code to write the protobuf:
var memoryStream = new MemoryStream();
await new ProtoBufFormatter().WriteToStreamAsync(typeof(T), obj, ms, null, null);
As soon as the object tree gets over a certain size (10000> and more items) - I end up with an OutOfMemoryException.
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?
Update I get the following exception:
Exception of type 'System.OutOfMemoryException' was thrown. at ProtoBuf.BufferPool.ResizeAndFlushLeft(Byte[]& buffer, Int32 toFitAtLeastBytes, Int32 copyFromIndex, Int32 copyBytes) in c:Devprotobuf-netprotobuf-netBufferPool.cs:line 60 at ProtoBuf.ProtoWriter.DemandSpace(Int32 required, ProtoWriter writer) in c:Devprotobuf-netprotobuf-netProtoWriter.cs:line 501 at ProtoBuf.ProtoWriter.WriteString(String value, ProtoWriter writer) in c:Devprotobuf-netprotobuf-netProtoWriter.cs:line 600 at proto_29(Object , ProtoWriter ) at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Write(Object value, ProtoWriter dest) in c:Devprotobuf-netprotobuf-netSerializersCompiledSerializer.cs:line 53 at ProtoBuf.Meta.RuntimeTypeModel.Serialize(Int32 key, Object value, ProtoWriter dest) in c:Devprotobuf-netprotobuf-netMetaRuntimeTypeModel.cs:line 752 at ProtoBuf.ProtoWriter.WriteObject(Object value, Int32 key, ProtoWriter writer) in c:Devprotobuf-netprotobuf-netProtoWriter.cs:line 46 at proto_27(Object , ProtoWriter ) at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Write(Object value, ProtoWriter dest) in c:Devprotobuf-netprotobuf-netSerializersCompiledSerializer.cs:line 53 at ProtoBuf.Meta.RuntimeTypeModel.Serialize(Int32 key, Object value, ProtoWriter dest) in c:Devprotobuf-netprotobuf-netMetaRuntimeTypeModel.cs:line 752 at ProtoBuf.ProtoWriter.WriteObject(Object value, Int32 key, ProtoWriter writer) in c:Devprotobuf-netprotobuf-netProtoWriter.cs:line 46 at proto_23(Object , ProtoWriter ) at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Write(Object value, ProtoWriter dest) in c:Devprotobuf-netprotobuf-netSerializersCompiledSerializer.cs:line 53 at ProtoBuf.Meta.RuntimeTypeModel.Serialize(Int32 key, Object value, ProtoWriter dest) in c:Devprotobuf-netprotobuf-netMetaRuntimeTypeModel.cs:line 752 at ProtoBuf.ProtoWriter.WriteObject(Object value, Int32 key, ProtoWriter writer) in c:Devprotobuf-netprotobuf-netProtoWriter.cs:line 46 at proto_9(Object , ProtoWriter ) at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Write(Object value, ProtoWriter dest) in c:Devprotobuf-netprotobuf-netSerializersCompiledSerializer.cs:line 53 at ProtoBuf.Meta.RuntimeTypeModel.Serialize(Int32 key, Object value, ProtoWriter dest) in c:Devprotobuf-netprotobuf-netMetaRuntimeTypeModel.cs:line 752 at ProtoBuf.ProtoWriter.WriteObject(Object value, Int32 key, ProtoWriter writer) in c:Devprotobuf-netprotobuf-netProtoWriter.cs:line 46 at proto_6(Object , ProtoWriter ) at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Write(Object value, ProtoWriter dest) in c:Devprotobuf-netprotobuf-netSerializersCompiledSerializer.cs:line 53 at ProtoBuf.Meta.RuntimeTypeModel.Serialize(Int32 key, Object value, ProtoWriter dest) in c:Devprotobuf-netprotobuf-netMetaRuntimeTypeModel.cs:line 752 at ProtoBuf.ProtoWriter.WriteRecursionSafeObject(Object value, Int32 key, ProtoWriter writer) in c:Devprotobuf-netprotobuf-netProtoWriter.cs:line 75 at proto_5(Object , ProtoWriter ) at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Write(Object value, ProtoWriter dest) in c:Devprotobuf-netprotobuf-netSerializersCompiledSerializer.cs:line 53 at ProtoBuf.Meta.RuntimeTypeModel.Serialize(Int32 key, Object value, ProtoWriter dest) in c:Devprotobuf-netprotobuf-netMetaRuntimeTypeModel.cs:line 752 at ProtoBuf.Meta.TypeModel.SerializeCore(ProtoWriter writer, Object value) in c:Devprotobuf-netprotobuf-netMetaTypeModel.cs:line 186 at ProtoBuf.Meta.TypeModel.Serialize(Stream dest, Object value, SerializationContext context) in c:Devprotobuf-netprotobuf-netMetaTypeModel.cs:line 217 at WebApiContrib.Formatting.ProtoBufFormatter.WriteToStreamAsync(Type type, Object value, Stream stream, HttpContent content, TransportContext transportContext)链接地址: http://www.djcxy.com/p/62806.html