【发布时间】:2015-04-06 12:24:07
【问题描述】:
我遇到了内存泄漏问题。泄漏来自这里:
public static BitmapSource BitmapImageFromFile(string filepath)
{
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.CacheOption = BitmapCacheOption.OnLoad; //here
bi.CreateOptions = BitmapCreateOptions.IgnoreImageCache; //and here
bi.UriSource = new Uri(filepath, UriKind.RelativeOrAbsolute);
bi.EndInit();
return bi;
}
我有一个ScatterViewItem,里面包含一个Image,来源就是这个函数的BitmapImage。
实际情况比这复杂得多,所以我不能简单地将图像放入其中。我也不能使用默认加载选项,因为图像文件可能会被删除,因此在删除过程中访问文件时会遇到一些权限问题。
当我关闭ScatterViewItem 时出现问题,这反过来又关闭了Image。但是,缓存的内存没有被清除。所以经过多次循环,内存消耗是相当大的。
我尝试在Unloaded函数中设置image.Source=null,但没有清除。
卸载时如何正确清空内存?
【问题讨论】:
-
谢谢,但遗憾的是 GC 没有收集到它。直接调用 GC.Collect() 也没有收集到。