.Net 3.5 中 BinaryFormatter 类的内存泄漏问题
我在 .Net 3.5 框架中看到 BinaryFormatter 类存在内存泄漏问题。 我正在使用以下方法来反序列化对象。传递给此方法的字节数组的大小为 156MB。然而,我调用这个方法后,有2.6GB的跳跃。
我在 MSDN 上找到了这篇文章,讨论了这个内存泄漏问题。 http://blogs.msdn.com/b/psirr/archive/2009/11/13/interesting-memory-leak-in-net-3-5-binary-deserialization.aspx
有人吗知道这个问题是否有解决办法? .Net 4.0 中解决了吗?
public static Message DeserializeContent(byte[] content)
{
var formatter = new BinaryFormatter();
Message message;
using (var stream = new MemoryStream(content))
{
message = (Message) formatter.Deserialize(stream);
stream.Close();
}
return message;
}
I am seeing memory leak issue with BinaryFormatter class in .Net 3.5 framework.
I am using following method to deserialize an object. The size of the byte array passed to this method is 156MB. However, there is jump of 2.6GB after I call this method.
I found this article on MSDN that talks about this memory leak issue. http://blogs.msdn.com/b/psirr/archive/2009/11/13/interesting-memory-leak-in-net-3-5-binary-deserialization.aspx
Does anyone know if there is a solution to this problem? Is it resolved in .Net 4.0?
public static Message DeserializeContent(byte[] content)
{
var formatter = new BinaryFormatter();
Message message;
using (var stream = new MemoryStream(content))
{
message = (Message) formatter.Deserialize(stream);
stream.Close();
}
return message;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那很有意思。内存不是受管理的吗,即使发生泄漏,也不应该被收集吗?
即使您处理二进制格式化程序或强制垃圾收集器运行,也会发生这种情况吗?
然而,即使它确实收集了泄漏的内存,请记住这不是一个绝对的解决方案,因为在内存被处置之前可能没有足够的内存来反序列化......
希望它能得到修复。
That's interesting. isn't the memory managed and even in case of a leak, shouldn't it be collected?
Does it happen even if you dispose the binary formatter or force the garbage collector to run?
However, even if it does collect the leaked memory, keep in mind that this isn't an absolute solution as there might not be enough memory to deserialize before the memory can be disposed...
Hope it will get fixed.