我使用protobuf将一些大的Dictionary序列化到磁盘上,磁盘上的文件大小为450 on。
通常,代码在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捕捉异常,并在几秒钟后重试。而且它们通常起作用。
有办法避免这种异常吗?
发布于 2015-06-15 11:51:43
gcAllowVeryLargeObjects在字节缓冲区方面没有多大帮助,因为元素编号限制仍然适用--但是,奇怪的是,它正在以450 me的速度崩溃;不要误解我的意思--这是相当大的。您是在序列化到文件吗?网络?或者只是记忆中的?如果它不是纯内存中的,那么可能有办法说服它减少内部缓冲--例如,切换到组而不是子消息(这只需要一些属性更改)。例如:这个
public class Foo {
[ProtoMember(1)]
public List<Bar> Bars {get; set;}
}需要在内存中缓冲Bar数据,其中-as:
public class Foo {
[ProtoMember(1, DataFormat = DataFormat.Group)]
public List<Bar> Bars {get; set;}
}不会的。
发布于 2018-11-29 15:05:12
我们的应用程序中也有同样的问题,试图通过WCF将3百万条记录从服务端传递到WPF客户端。
解决方案是将序列化进行序列化的服务端重新构建为64位应用程序(不是AnyCPU,而是x64),并确保APPPool以64位模式运行(“允许32位应用程序”设置为False)。
享受吧!
https://stackoverflow.com/questions/30844224
复制相似问题