内存泄漏更改Windows Phone 7中的图像

ppa*_*ojr 3 c# silverlight memory-leaks windows-phone-7

当我在Windows Phone 7.5中多次更改图像容器的图像时,我遇到了问题

这是错误的代码:

public void displayImages() {
    image1.Source = new System.Windows.Media.Imaging.BitmapImage
       (new Uri("BrainImg/axis/" + axis + currentSlice + ".jpg",
             UriKind.RelativeOrAbsolute));
    image2.Source = new System.Windows.Media.Imaging.BitmapImage
       (new Uri("BrainImg/aseg/" + axis + currentSlice + ".png",
             UriKind.RelativeOrAbsolute));
}

private void slider1_ValueChanged(object sender, 
                                  RoutedPropertyChangedEventArgs<double> e)
{
    // do something
    if (this.slider1 != null)
    {
        currentSlice = (int) this.slider1.Value;
        displayImages();
    }
}
Run Code Online (Sandbox Code Playgroud)

经过一些更改(大约100我内存不足)

我已经尝试设置image.Sourcenull指定新值之前.

Mat*_*cey 7

Image控件的默认行为是缓存图像以供将来重用.这意味着控制器仍在使用内存.您需要显式释放对图像的引用以释放内存

像这样:

  BitmapImage bitmapImage = image.Source as BitmapImage;
  bitmapImage.UriSource = null;
  image.Source = null;
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请访问:http://blogs.msdn.com/b/swick/archive/2011/04/07/image-tips-for-windows-phone-7.aspx