1

I have a 3.5 WPF application that use's RenderTargetBitmap.

It eat's memory like a big bear.

It's is a know problem in 3.5 that RenderTargetBitmap.Render has memory problems.

Have find some solutions for it, but i doesnt help. https://connect.microsoft.com/VisualStudio/feedback/details/489723/rendertargetbitmap-render-method-causes-a-memory-leak

Program takes too much memory And more...

Does anyway have any more ideas to solve it...

static Image Method(FrameworkElement e, int width, int height)
{
    const int dpi = 192;

    e.Width = width;
    e.Height = height;

    e.Arrange(new Rect(0, 0, width, height));
    e.UpdateLayout();

    if(element is Graph)
        (element as Graph).UpdateComponents();

    var bitmap = new RenderTargetBitmap((int)(width*dpi/96.0),
                                                     (int)(height*dpi/96.0),
                                                     dpi,
                                                     dpi,
                                                     PixelFormats.Pbgra32);

    bitmap.Render(element);

    var encoder = new PngBitmapEncoder();

    encoder.Frames.Add(BitmapFrame.Create(bitmap));

    using (var stream = new MemoryStream())
    {
        encoder.Save(stream);
        element.Clip = null;
        Dispose(element);
        bitmap.Freeze();

        DisposeRender(bitmap);
        bitmap.Clear();

        GC.Collect();
        GC.WaitForPendingFinalizers();
        return System.Drawing.Image.FromStream(stream);

    }
}

public static void Dispose(FrameworkElement element)
{
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();
}

public static void DisposeRender(RenderTargetBitmap bitmap)
{
        if (bitmap != null) bitmap.Clear();
        bitmap = null;
        GC.Collect();
        GC.WaitForPendingFinalizers();
}
Community
  • 1
  • 1
kingRauk
  • 1,259
  • 1
  • 11
  • 24
  • This code needs a lot of comment. What does each action do, where do you copy/create a bitmap and where do you dispose what element and why. Is there a real memory leak or does the application simply use whatever is available? – CodingBarfield Jun 01 '12 at 09:19
  • The metod creates images that is later shown in a pdf. The problem is that foreach time i run this metod the memory consumption increases and even after i am done with the pdf it doesnt release any memory. – kingRauk Jun 01 '12 at 09:40
  • It could be a simple problem like a static list keeping a reference to all the images. Try and change the 'global' implementation and check if there are ways to keep the memory usage constant. Calling GC.Collect() twice should clean up all memory usage. (oh and upvote helpfull answers/comments) – CodingBarfield Jun 01 '12 at 09:44
  • @kingRauk Could you clarify this sentence: `It is a known problem in 3.5`? Where can i find some reference about this? – gliderkite Jun 01 '12 at 10:24
  • What i mean is that RenderTargetBitmap.Render has problems with memory leaks in 3.5. https://connect.microsoft.com/VisualStudio/feedback/details/489723/rendertargetbitmap-render-method-causes-a-memory-leak – kingRauk Jun 01 '12 at 10:35

1 Answers1

1

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

You cant even release the Used Memory of RenderTargetBitmap.

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

        GC.Collect()
        GC.WaitForPendingFinalizers()
        GC.Collect()
GiGatR00n
  • 116
  • 8