问题描述
大家好,
我的代码中存在大量内存泄漏,一段时间后会引发该错误并导致内存错误.
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
推荐答案
这篇关于使用RenderTargetBitmap的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!