Question

I'm a little baffled by a memory leak in my WPF code. I'm rendering some 3D geometry to several RenderTargetBitmaps, then rendering each of those to a large, master RenderTargetBitmap. But when I do this, I get a memory leak that crashes my app after just a minute or two.

I've reproduced the error in the following simplified piece of code.

   private void timer1_Tick(object sender, EventArgs e) {
       // if first time, create final stitch bitmap and set UI image source
       if (stitch == null) {
           stitch = new RenderTargetBitmap(1280, 480, 96, 96, PixelFormats.Pbgra32);
           myImage.Source = stitch;
       }

       // create visual and render to img1
       Rect rect = new Rect(new Point(160, 100), new Size(320, 80));
       DrawingVisual dvis = new DrawingVisual();
       using (DrawingContext dc = dvis.RenderOpen()) {
           dc.DrawRectangle(System.Windows.Media.Brushes.LightBlue, (System.Windows.Media.Pen)null, rect); 
       }
       RenderTargetBitmap img1 = new RenderTargetBitmap(640, 480, 96, 96, PixelFormats.Pbgra32);
       img1.Render(dvis);

       // create visual and render to final stitch
       DrawingVisual vis = new DrawingVisual();
       using (DrawingContext dc = vis.RenderOpen()) {
           dc.DrawImage(img1, new Rect(0, 0, 640, 480));
       }

       stitch.Clear();
       stitch.Render(vis);
   }   

Can anyone see anything obvious that is going wrong here? Why would this code have an egregious memory leak?

Was it helpful?

Solution

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()

This maybe solve your problem:

private void timer1_Tick(object sender, EventArgs e) {
       // if first time, create final stitch bitmap and set UI image source
       if (stitch == null) {
           stitch = new RenderTargetBitmap(1280, 480, 96, 96, PixelFormats.Pbgra32);
           myImage.Source = stitch;
       }

       // create visual and render to img1
       Rect rect = new Rect(new Point(160, 100), new Size(320, 80));
       DrawingVisual dvis = new DrawingVisual();
       using (DrawingContext dc = dvis.RenderOpen()) {
           dc.DrawRectangle(System.Windows.Media.Brushes.LightBlue, (System.Windows.Media.Pen)null, rect); 
       }
       RenderTargetBitmap img1 = new RenderTargetBitmap(640, 480, 96, 96, PixelFormats.Pbgra32);
       img1.Render(dvis);

       // create visual and render to final stitch
       DrawingVisual vis = new DrawingVisual();
       using (DrawingContext dc = vis.RenderOpen()) {
           dc.DrawImage(img1, new Rect(0, 0, 640, 480));
       }

        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();
       stitch.Clear();
       stitch.Render(vis);
}   
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top