WPF 图像控件内存泄漏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13611421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Image control memory leak
提问by Cracker
My program has a lotof small images (Image controls are small, not the images themselves) and by saying a lot I mean more than 500. These images are generated asynchronously and then assigned to the Imagecontrols, which were initialized before.
Basically my code does the following:
我的程序有很多小图像(图像控件很小,而不是图像本身),说很多我的意思是超过 500 个。这些图像是异步生成的,然后分配给Image控件,这些控件之前已初始化。
基本上我的代码执行以下操作:
filename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, string.Format("{0}.JPG", Guid.NewGuid().GetHashCode().ToString("x2")));
converter.ConvertPdfPageToImage(filename, i);
//Fire the ThumbnailCreated event
onThumbnailCreated(filename, (i - 1));
There is no memory leak in code that creates the images, I have the following code:
创建图像的代码中没有内存泄漏,我有以下代码:
string[] files = Directory.GetFiles("C:\Users\Daniel\Pictures", "*.jpg");
for(int i=0; i<files.Length; i++){
onThumbnailCreated(files[i], i);
}
Still the problem persists. This happens in the event handler method:
问题仍然存在。这发生在事件处理程序方法中:
void Thumbnails_ThumbnailCreated(ThumbnailCreatedEventArgs e, object sender)
{
//Since we generate the images async, we need to use Invoke
this.parent.Dispatcher.Invoke(new SetImageDelegate(SetImage), e.Filename, e.PageNumber);
}
private void SetImage(string filename, int pageNumber)
{
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
//I am trying to make the Image control use as less memory as possible
//so I prevent caching
bitmap.CacheOption = BitmapCacheOption.None;
bitmap.UriSource = new Uri(filename);
bitmap.EndInit();
//We set the bitmap as the source for the Image control
//and show it to the user
this.images[pageNumber].Source = bitmap;
}
With 468 images the program uses about 1Gb of memory and then runs out of it at all. Is my task even possible to achieve using WPF or is the number of images too high? Maybe there is something wrong with my code?
Thanks in advance
对于 468 个图像,该程序使用大约 1Gb 的内存,然后完全用完。我的任务甚至可以使用 WPF 来实现还是图像数量太高?也许我的代码有问题?
提前致谢
回答by user7116
You should freeze these imagesand set their width (or height)to that will be actually used in the application if possible:
如果可能,您应该冻结这些图像并将它们的宽度(或高度)设置为将在应用程序中实际使用的值:
// ...
bitmap.DecodePixelWidth = 64; // "displayed" width, this improves memory usage
bitmap.EndInit();
bitmap.Freeze();
this.images[pageNumber].Source = bitmap;
回答by Shai Cohen
It may be the event handlers that are causing your memory leaks.
可能是导致内存泄漏的事件处理程序。
See this SO question:
看到这个问题:
回答by Ben Walker
Try this:
尝试这个:
private void SetImage(string filename, int pageNumber)
{
using (BitmapImage bitmap = new BitmapImage())
{
bitmap.BeginInit();
//I am trying to make the Image control use as less memory as possible
//so I prevent caching
bitmap.CacheOption = BitmapCacheOption.None;
bitmap.UriSource = new Uri(filename);
bitmap.EndInit();
this.images[pageNumber].Source = bitmap;
}
}
That will dispose of your bitmaps when you're done with them.
当你完成它们时,这将处理你的位图。

