【发布时间】:2015-06-15 11:38:06
【问题描述】:
我通过protobuf-net将一些大的Dictionary序列化到磁盘,磁盘上的文件大小为450MB。
通常,代码因 OutOfMemoryException 而失败
System.OutOfMemoryException: 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.StartSubItem(Object instance, ProtoWriter writer, Boolean allowFixed) in c:\Dev\protobuf-net\protobuf-net\ProtoWriter.cs:line 341
at ProtoBuf.ProtoWriter.WriteObject(Object value, Int32 key, ProtoWriter writer) in c:\Dev\protobuf-net\protobuf-net\ProtoWriter.cs:line 44
at proto_15(Object , ProtoWriter )
at ProtoBuf.ProtoWriter.WriteObject(Object value, Int32 key, ProtoWriter writer, PrefixStyle style, Int32 fieldNumber) in c:\Dev\protobuf-net\protobuf-net\ProtoWriter.cs:line 116
at ProtoBuf.Meta.TypeModel.SerializeWithLengthPrefix(Stream dest, Object value, Type type, PrefixStyle style, Int32 fieldNumber, SerializationContext context) in c:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:line 548
at ProtoBuf.Meta.TypeModel.SerializeWithLengthPrefix(Stream dest, Object value, Type type, PrefixStyle style, Int32 fieldNumber) in c:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:line 515
at ProtoBuf.Serializer.SerializeWithLengthPrefix[T](Stream destination, T instance, PrefixStyle style, Int32 fieldNumber) in c:\Dev\protobuf-net\protobuf-net\Serializer.cs:line 352
机器有 20GB+ 可用内存。
而我的进程只占用 4.6GB
app.config 针对大内存进行了调整
<runtime>
<gcAllowVeryLargeObjects enabled="true" />
<gcServer enabled="true"/>
</runtime>
目前我使用try-catch 来捕获异常并在几秒钟后重试。而且它们通常有效。
有没有办法避免这个异常?
【问题讨论】:
标签: protobuf-net