[英]C# serializing/deserializing with memory stream System.OutOfMemoryException
I am trying to serialize/de-serialize a stream of around 50MB xml data with the following code and I get System.OutOfMemoryException exception. 我正在尝试使用以下代码序列化/反序列化约50MB xml数据流,并且出现System.OutOfMemoryException异常。
var formatter = new BinaryFormatter();
var stream = new MemoryStream();
using (stream)
{
formatter.Serialize(stream, source);
stream.Seek(0, SeekOrigin.Begin);
return (T) formatter.Deserialize(stream);
}
I debugged the code and it throws OutOfMemoryException exception on the formatter.Serialize(stream,source) line. 我调试了代码,并在formatter.Serialize(stream,source)行上引发OutOfMemoryException异常。
I did some search in it says that limit is 2GB. 我在其中进行了一些搜索,说限制为2GB。 How can I debug to find out the reason or is there any efficent way of writing this code? 如何调试以找出原因,或者有任何有效的方式编写此代码? Or any tool to watch the memory usage. 或使用任何工具来查看内存使用情况。
Thanks, 谢谢,
Dealing with 2GB of xml is never going to be efficient. 处理2GB的xml 永远不会高效。 However, to make it work , you could try writing to a FileStream
instead of a MemoryStream
, since a MemoryStream
has a 2GB limit. 但是,要使其工作 ,您可以尝试写入FileStream
而不是MemoryStream
,因为MemoryStream
的限制为2GB。 Alternatively, you could write your own Stream
implementation using multiple buffers rather than a single large buffer. 另外,您可以使用多个缓冲区而不是单个大缓冲区来编写自己的Stream
实现。
However, I strongly suggest that what you actually want to do here is some combination of: 但是,我强烈建议您在这里实际要做的是以下各项的组合:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.