RenderTargetBitmap内存泄漏 [英] RenderTargetBitmap Memory Leak

查看:398
本文介绍了RenderTargetBitmap内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

am尝试使用RenderTargetBitmap渲染图像 每次我从RenderTargetBitmap创建一个实例来渲染图像时,内存都会增加,完成后,内存永远不会释放 这是代码:

am trying to render an image with RenderTargetBitmap every time i create an instance from RenderTargetBitmap to render image the memory increased and when am done the memory never released and this is the code :

RenderTargetBitmap rtb = new RenderTargetBitmap((int)(renderWidth * dpiX / 96.0),
                                                (int)(renderHeight * dpiY / 96.0),
                                                dpiX,
                                                dpiY,
                                                PixelFormats.Pbgra32);
    DrawingVisual dv = new DrawingVisual();
    using (DrawingContext ctx = dv.RenderOpen())
    {
       VisualBrush vb = new VisualBrush(target);
       ctx.DrawRectangle(vb, null, new System.Windows.Rect(new Point(0, 0), new Point(bounds.Width, bounds.Height)));
    }
    rtb.Render(dv);

请我需要帮助 我该如何释放记忆 并感谢大家.

please i need help how can i release the memory and thanks for all.

推荐答案

如果使用资源监视器监视RenderTargetBitmap类的行为,则每次看到该类时,您会看到丢失500KB你的记忆.我对您问题的回答是:不要多次使用RenderTargetBitmap

if you monitor behaviors of the RenderTargetBitmap class using Resource Monitor, you can see each time this class called, you lose 500KB of your memory. my Answer to your Question is: Dont use RenderTargetBitmap class so many times

您不能释放RenderTargetBitmap的已用内存.

You cant event release the Used Memory of RenderTargetBitmap.

如果您确实需要使用RenderTargetBitmap类,只需在代码末尾添加这些行.

If you really need using RenderTargetBitmap class, just add these lines at End of your code.

        GC.Collect()
        GC.WaitForPendingFinalizers()
        GC.Collect()

这可以解决您的问题:

    RenderTargetBitmap rtb = new RenderTargetBitmap((int)(renderWidth * dpiX / 96.0),
                                                    (int)(renderHeight * dpiY / 96.0),
                                                    dpiX,
                                                    dpiY,
                                                    PixelFormats.Pbgra32);
        DrawingVisual dv = new DrawingVisual();
        using (DrawingContext ctx = dv.RenderOpen())
        {
           VisualBrush vb = new VisualBrush(target);
           ctx.DrawRectangle(vb, null, new System.Windows.Rect(new Point(0, 0), new Point(bounds.Width, bounds.Height)));
        }
        rtb.Render(dv);

        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();

这篇关于RenderTargetBitmap内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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