I'm facing a memory leak issue. The leak comes from here:
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;
}
I have a ScatterViewItem
, which contains an Image
, and the source is the BitmapImage
of this function.
The actual thing is a lot more complex than this, so I can't simply put an Image into it. I also can't use the default load options, as the image file might be deleted and hence will face some permission issue accessing the file during deletion.
The problem occurs when I close the ScatterViewItem
, which in turn closes the Image
. However, the cached memory isnt cleared. So after many cycles, the memory consumption is pretty big.
I tried setting image.Source=null
during the Unloaded
function, but it didn't clear it.
How do I clear the memory correctly during unloading?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…