【问题标题】:WP8 Memory leak opening and closing PhoneApplicationPageWP8 内存泄漏打开和关闭PhoneApplicationPage
【发布时间】:2013-07-05 15:57:32
【问题描述】:

我正在创建一个显示缩略图列表的 Windows 手机应用程序。我正在使用 LongListSelector 来执行此操作。

当我向前和向后导航到缩略图列表时,我的应用程序出现内存泄漏。我在使用该应用程序时查看了内存使用情况,我发现在打开带有缩略图的页面时内存增加了(正如我所期望的那样)。当我导航回上一页时,内存使用量减少了,但没有增加那么多。多次重复该过程会导致内存不足异常。

我创建了一个只有两个页面的测试应用程序。一个带有按钮导航到另一个在 LongListSelector 中加载一组 Poto 的按钮。我创建这个应用程序是为了确保内存泄漏不是由其他原因引起的。

在这个简单的测试中,内存使用的行为与我的应用程序中的行为一样。

这是我的缩略图页面的主要代码:

public class testObject
{
    public string Title { get; set; }
    public BitmapImage Thumbnail { get; set; }
}

protected override void OnNavigatedTo(NavigationEventArgs e)
{

    photosList = new List<testObject>();
    for (int i = 0; i < 200; i++)
    {
        BitmapImage bi = new BitmapImage(new Uri("/images/"
                                        + i.ToString()+".jpg",
                                        UriKind.RelativeOrAbsolute));


        photosList.Add(new testObject { Title = i.ToString(),
                                            Thumbnail = bi });
    }

    GridPictures.ItemsSource = photosList;

}

protected override void OnBackKeyPress(
            System.ComponentModel.CancelEventArgs e)
{
    foreach (testObject test in photosList)
    {
        test.Thumbnail.DecodePixelHeight = 1;
        test.Thumbnail.DecodePixelWidth = 1;
        test.Thumbnail = null;
    }
    photosList.Clear();
    photosList = null;

    base.OnBackKeyPress(e);
}

这是另一个页面上按钮的代码:

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.RelativeOrAbsolute));

}

【问题讨论】:

标签: c# memory-leaks windows-phone-8 out-of-memory longlistselector


【解决方案1】:

LongListSelector 是已知的泄漏源。当您使用像 Image 这样的控件时,这些泄漏会变得特别麻烦,因为它会占用大量内存。

到目前为止,最好的解决方案是完全避免 LongListSelector。但是如果您找不到合适的替代方案,您有一些解决方法:

  • 在Page1,OnNavigatedFrom事件中,强制解放图片。有几种方法可以做到这一点,但通常将 ImageSource 属性设置为 null 就足够了

  • 制作一个自定义控件来为您完成繁琐的工作

自定义控件可能如下所示:

public class SafePicture : System.Windows.Controls.ContentControl
{
    public SafePicture()
    {
        this.Unloaded += this.SafePictureUnloaded;
    }

    private void SafePictureUnloaded(object sender, System.Windows.RoutedEventArgs e)
    {
        var image = this.Content as System.Windows.Controls.Image;

        if (image != null)
        {
            image.Source = null;
        }
    }
}

然后,只需将所有图片包装在该控件中即可:

<my:SafePicture>
    <Image Source="{Binding Path=Thumbnail}" />
</my:SafePicture>

这应该可以解决问题。请注意,您仍然会泄漏内存,但泄漏量要合理得多。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-26
    • 2023-03-15
    • 2015-03-07
    • 2013-05-17
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2017-12-01
    相关资源
    最近更新 更多