如何序列化的.NET大对象? (内存溢出的例外) [英] How to serialize big objects in .NET? (OutOfMemory Exceptions)

查看:1414
本文介绍了如何序列化的.NET大对象? (内存溢出的例外)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的序列化在我的应用保存功能。但是,当数据量太大(15 + MB),我开始变得内存不足的异常。

I'm using serialization for "save" feature in my application. But when the data is too big (15+ MB) I'm starting to get OutOfMemory exceptions.

我有这么多的对象以及它们与其他小物体相连,我认为这是导致在存储器中保存太多的处理能力和数据。

I've got so many objects and they are connected with other little objects, I think this is causing too much processing power and data held in the memory.

我的code基于此,几乎相同的:

My code is based on this, almost same:

HTTP://www.$c$cproject.com/KB/ VB / TreeViewDataAccess.aspx

编辑:

  1. 我不使用自定义序列,它全部由[连载]属性。不计入某些领域。

  1. I don't use custom serialization, it's all done by [Serialization] attributes. Excluding some fields.

我序列这么多的对象和自定义类。包括字典,结构和一堆其他的东西。

I serialize so many objects and custom classes. Includes Dictionary, structures and bunch of other stuff.

我序列化到一个文件中。

I serialize it into a file.

我用的XmlSerializer

I use XmlSerializer

P.S。我有4 GB的物理内存。

解决方案

感谢答案,我的问题被认为是与XmlSerializer的,我已经摆脱了它。二进制序列化是工作只是罚款我已经得到的数据。

Thanks to answers, my problem was found to be with XmlSerializer and I've got rid of it. Binary serialization is working just fine with the data I've got.

推荐答案

15MB不应该给你一个OOM。

15MB shouldn't give you an OOM.

如果数据是树状(而不是一个完整的图形),你可能会考虑类似的 protobuf网;以及使用谷歌的效率非常高(速度和内存)的二进制协议缓冲区的格式,它得益于不必做参考追踪(用于图形) - 这意味着只需要担心数据一次(两次,如果它有得到缓冲)。

If the data is tree-like (rather than a full graph), you might consider a serializer like protobuf-net; as well as using Google's very efficient (both speed and memory) binary "protocol buffers" format, it benefits from not having to do reference tracking (required for graphs) - which means it only has to worry about data once (twice if it has to get buffered).

不过,这需要不同的标记,以你的类(或至少是一个选择加入) - 它不会处理全图​​。但它的存在,以及免费...

However, this requires different markup to your classes (or at least, an "opt in") - and it won't handle full graphs. But it is there, and free...

这篇关于如何序列化的.NET大对象? (内存溢出的例外)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆