使用RenderTargetBitmap的内存泄漏 [英] Memory leak using RenderTargetBitmap

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

问题描述

大家好,

我的代码中存在大量内存泄漏,一段时间后会引发该错误并导致内存错误.

Have Massive memory leak in in my code, which after a while is throwing and memory error.

该方法具有一个传递给它的网格,该网格及其内容被呈现并保存为png.问题在于此方法最多可以运行1000次,并且每次运行时内存使用量都会增加.我希望每次运行后都可以记忆 进行清理并再次使用?它创建的.png大小不大,每个都在5到100kb之间.您知道为什么内存会一直爬行直到崩溃吗?

The method has a Grid passed to it, this grid and its contents is rendered and saved out as a png. Problem is this method is run up to a 1000 times, and each time it runs the memory usage increases. I expected after each time its run for the memory to be cleaned up and made available again?. The .pngs it creates are not big, between 5 and 100kb each. Do you know why the memory would keep creeping up until crashing?

 

public void savePlayerToFile(Grid __item, String __savePath, String __fileNameIn)

  {

   __item.UpdateLayout();

   __item.Arrange(new Rect());

   __item.Measure(new Size((int)__item.ActualWidth, (int)__item.ActualHeight));

   __item.Arrange(new Rect(new Size((int)__item.ActualWidth, (int)__item.ActualHeight)));

   RenderTargetBitmap bitmap = new RenderTargetBitmap((int)__item.ActualWidth, (int)__item.ActualHeight, 0, 0,

               PixelFormats.Pbgra32);

   bitmap.Render(__item);

   using (FileStream outStream = new FileStream(__savePath.ToLower() + @"\" + __fileNameIn + ".png", FileMode.Create))

   {

    PngBitmapEncoder enc = new PngBitmapEncoder();

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

    enc.Save(outStream);

   }

   bitmap.Freeze();

   GC.Collect();

   bitmap = null;

  }

欢呼


tommy


tommy

推荐答案

每次执行此操作时是否显示该窗口\页面,如果是,您是否取消订阅该Window \ Page所订阅的所有事件?另外,您是否看到过以下内容: http://support.microsoft.com/kb/938416
Is the window\page shown each time you do this and if so are you unsubscribing to all events that this Window\Page subscribes to? Also, have you seen this: http://support.microsoft.com/kb/938416


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

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