2

Stefan Wick 在他的博客中写道,我们可以使用类似的代码清除 WP7 中的图像缓存

<code>
  BitmapImage bitmapImage = image.Source as BitmapImage;
  bitmapImage.UriSource = null;
  image.Source = null;

http://blogs.msdn.com/b/swick/archive/2012/04/05/10151249.aspx?CommentPosted=true#commentmessage

我已经测试了他的示例——项目(ImageTips.zip)——我可以说上面的代码不起作用——它不会释放缓存图像的内存,也不会删除应用程序的缓存图像。

当您将 UriSource 设置为 null 时——您只会释放页面 Caching.xaml 上的内存,但如果您将导航回 MainPage.xaml --- 您将看到内存没有释放 --- 内存增加了!!!

看到我们可以使用 Stefan 的项目 --- ImageTips.zip in his blog...

测试项目 ImageTips.zip

重现我的观察 --- 您可以: 1. 将代码添加到 MainPage.xaml 以查看当前内存值,例如在 Caching.xaml 页面上:

<tab>

        DispatcherTimer timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromMilliseconds(500);
        timer.Start();
        timer.Tick += delegate
        {
            GC.Collect();
            tbMemory.Text = string.Format("Memory: {0} bytes", DeviceExtendedProperties.GetValue("ApplicationCurrentMemoryUsage"));
        };
</code>
  1. 运行您的应用程序。当我们第一次导航到 MainPage.xaml --- 内存值接近 9676448 字节
  2. 点击导航到 Caching.xaml(图像缓存)---内存值接近 9011200 字节
  3. 单击按钮 Show 以显示图像 -- 内存值接近 12996608 字节
  4. 单击避免图像缓存(清除缓存的图像),然后按下按钮清除 --- 10090144 字节 .... 那么,413696 字节在哪里消失了???(10090144 - 9676448 = 413696 字节)
  5. 单击按钮返回以导航回 MainPage.xaml --- 11828248 字节...但是导航到 Caching.xaml 之前的先前值是 9676448 字节...那么,哪里消失了 2151800 字节??

    如果您将导航 20 次到页面 Caching.xaml(单击显示图像并使用缓存清除图像)并且当导航回 MainPage.xaml --- 使用的内存将增加到 3.3 mb...等等

我在我的应用程序中遇到了这个问题,不知道如何解决。

在 Windows phone 7 中清除图像缓存的其他变体有哪些?或者为什么通过将 UriSource 设置为 null 来清除图像缓存 --- 导航回上一页时不起作用(不释放内存)?

谢谢。

4

1 回答 1

6

我可以向您保证,博客中给出的示例是有效的。请注意,您在示例中处理的内存量非常小,您是否在这里或那里“丢失”了 2MB 并不重要。这可能是因为 GC 尚未启动,或者同时分配了一些其他对象。(如果您确实遇到了严重的泄漏,那么您必须检查代码中的其他地方)

在我自己的应用程序中,我正在处理大量图像。在任何给定时间,它们都会消耗多达 60 MB 的内存。因此,我非常偏执地没有任何内存泄漏来通过认证。

博客中的示例假定您正在以编程方式添加图像,并且您可以访问它们,因此您可以将源设置为 null。

  BitmapImage bitmapImage = image.Source as BitmapImage;
  bitmapImage.UriSource = null;
  image.Source = null;

仅此一项就可以防止图像缓存。

但是,如果您使用数据绑定,您将很快意识到防止图像缓存可能并不容易。

在我使用数据绑定的应用程序中,我设置了类似于以下内容的内容:

假设您想在列表框中显示图像。

//MainPage.xaml
<StackPanel>
<ListBox ItemsSource="{Binding Pictures}">
    <ListBox.ItemTemplate>
       <DataTemplate>
          <Image MaxHeight="1000" Source="{Source}" />
       </DataTemplate>
    </ListBox.ItemTeplate>
</ListBox> 
<Button Tap="LoadImages_Tap" Content="Load Images" />
</StackPanel> 

-

 //Picture.cs
    public class Picture
    { 
        private BitmapImage _source = new BitmapImage()
        {
            CreateOptions = BitmapCreateOptions.BackgroundCreation | BitmapCreateOptions.IgnoreImageCache | BitmapCreateOptions.DelayCreation
        };

        public Picture(string url) {
            _source.UriSource = new Uri(url, UriKind.Absolute);
        }

        public BitmapImage Source
        {
            get
            {
                return _source;
            } 
        } 

    }
}


//MainPage.xaml.cs
private ObservableCollection<Picture> _pictures = new ObservableCollection<Picture>();
public ObservableCollection<Picture> Pictures {
  get {
   return _pictures;
  }
}
public MainPage() {
  //...
  DataContext = this;
}

protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
        if (e.NavigationMode == NavigationMode.Back)
        {
            PreventCaching();
            _pictures.Clear();
        }
        base.OnNavigatedFrom(e);
}  

private void PreventCaching() {
   foreach(var picture in _pictures) {
     picture.Source.UriSource = null;
   }
}
private void LoadPictures_Tap(object sender, EventArgs e) { 
     PreventCaching();
    _pictures.Clear();
    foreach(var url in SomeMethodThatReturnsUrlsForImages()) {  
      _pictures.Add(new Picture(url));
    }
}

当您使用数据绑定时,上面应该清除图像缓存(当没有直接访问 <Image> 元素时)

或者,您可以创建一个自定义附加依赖项属性,因此您可以这样做: <Image ext:ImageExtension.Source="{SomeSource}" /> 并在那里处理缓存的清除,但我不需要它。

于 2012-04-09T16:33:35.877 回答