.Net 3.5 中 BinaryFormatter 类的内存泄漏问题

发布于 2024-10-06 13:57:42 字数 809 浏览 0 评论 0原文

我在 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空城仅有旧梦在 2024-10-13 13:57:42

那很有意思。内存不是受管理的吗,即使发生泄漏,也不应该被收集吗?
即使您处理二进制格式化程序或强制垃圾收集器运行,也会发生这种情况吗?
然而,即使它确实收集了泄漏的内存,请记住这不是一个绝对的解决方案,因为在内存被处置之前可能没有足够的内存来反序列化......

希望它能得到修复。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文