序列化大对象树时发生OutOfMemoryException
我正在序列化具有这种结构的对象:
[ProtoContract]
public class Foo{
// some more properties
[ProtoMember(100)]
public IEnumerable<Bar>{get;set}
}
[ProtoContract]
public class Bar{
// a lot of properties
}
我正在使用此代码编写protobuf:
var memoryStream = new MemoryStream();
await new ProtoBufFormatter().WriteToStreamAsync(typeof(T), obj, ms, null, null);
只要对象树超过一定的大小(10000>以上的项目) - 我最终会出现OutOfMemoryException异常。
我想知道是否有办法告诉protobuf-net做更好的序列化(以避免OutOfMemoryException)或者是否有其他方法来解决这个问题?
更新我收到以下异常:
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/62805.html
上一篇: OutOfMemoryException when serializing large object tree