WP8内存泄露开幕式和闭幕式的PhoneApplicationPage [英] WP8 Memory leak opening and closing PhoneApplicationPage

查看:172
本文介绍了WP8内存泄露开幕式和闭幕式的PhoneApplicationPage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个Windows Phone应用程序,显示缩略图列表。我使用的是LongListSelector做到这一点。



我的应用程序有内存泄漏,当我向前和向后导航的缩略图列出一些时间。我查看内存使用量,而使用的应用程序,我看到内存使用缩略图打开页面时(我希望)增加。当我导航回先前的页面存储器使用率下降但不及在它增加。重复PROCES几次它在内存不足的异常结束。



我创建了一个测试应用程序,只有两页。一用一键导航到加载在LongListSelector一套波托西等。我创建了这个应用程序,以确保内存泄漏不会被别的东西造成的。



在这个简单的测试内存的使用行为,因为它在我的应用程序。



下面是我的页面的主要代码与thumbnais:

 公开一流的TestObject 
{
公共字符串名称{搞定;组; }
公共BitmapImage的缩略图{搞定;组; }
}

保护覆盖无效的OnNavigatedTo(NavigationEventArgs E)
{

photosList =新的List<&的TestObject GT;();
的for(int i = 0; I< 200;我++)
{
BitmapImage的双向=新的BitmapImage(新的URI(/图片/
+ i.ToString( )+。JPG,
UriKind.RelativeOrAbsolute));


photosList.Add(新的TestObject {标题= i.ToString(),
缩略图=双向});
}

GridPictures.ItemsSource = photosList;

}

保护覆盖无效OnBackKeyPress(
System.ComponentModel.CancelEventArgs E)
{
的foreach(在photosList的TestObject测试)
{
test.Thumbnail.DecodePixelHeight = 1;
test.Thumbnail.DecodePixelWidth = 1;
test.Thumbnail = NULL;
}
photosList.Clear();
photosList = NULL;

base.OnBackKeyPress(E);
}

和这里是按钮的其他页面上的代码:

 私人无效Button_Click_1(对象发件人,RoutedEventArgs E)
{
NavigationService.Navigate(新的URI(/第1页的.xaml,UriKind.RelativeOrAbsolute));

}


解决方案

LongListSelector 是泄漏的已知来源。这些泄漏当你使用像图片,它使用了大量的内存控制变得特别麻烦。



最好的解决方案,到目前为止,是完全避免LongListSelector。但是,你不能找到一个合适的替代品,你有几个解决方法:




  • 在第1页,在 OnNavigatedFrom 事件,迫使图片的解放。有几个方法可以做到这一点,但通常的ImageSource 属性设置为null是足够的。


  • 请自定义控件做肮脏的工作,为您




自定义控制可能看起来像:

 公共类SafePicture:System.Windows.Controls.ContentControl 
{
公共SafePicture()
{
this.Unloaded + = this.SafePictureUnloaded;
}

私人无效SafePictureUnloaded(对象发件人,System.Windows.RoutedEventArgs E)
{
VAR图像= this.Content为System.Windows.Controls.Image ;

如果(图像!= NULL)
{
image.Source = NULL;
}
}
}



然后,只是包装所有在控制图片:

 <我:SafePicture> 
<图像来源={绑定路径=缩略图}/>
< /我:SafePicture>

和应该做的伎俩。请注意,您仍然会泄漏内存,但在更为合理的金额。


I'm creating a Windows phone application that shows a list of thumbnails. I'm using a LongListSelector to do it.

My application has memory leaks when I navigate forward and backward to the thumbnails list some times. I review the memory usage while using the App and I see that the memory increases when opening the page with the thumbnails (as I expect to). When I navigate back to the previous page the memory usage decrease but not as much at it was increased. Repeating the proces several times it ends in an out of memory exception.

I created a test application that only has two pages. One with a button to navigate to other that loads a set of potos in a LongListSelector. I create this App to be sure that the memory leak is not caused by something else.

In this simple test the memory usage behaves as it does in my App.

Here is the main code of my page with the thumbnais:

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);
}

And here is the code of the button on the other page:

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

}

解决方案

LongListSelector is a known source of leaks. Those leaks become especially troublesome when you're using controls like Image, which uses a large amount of memory.

The best solution, so far, is to avoid LongListSelector altogether. But it you can't find a suitable alternative, you have a few workarounds:

  • In Page1, in the OnNavigatedFrom event, force the liberation of the pictures. There's a few ways to do that, but usually setting the ImageSource property to null is enough

  • Make a custom control to do the dirty work for you

The custom control could look like:

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;
        }
    }
}

Then, just wrap all your pictures in that control:

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

And that should do the trick. Note that you will still be leaking memory, but in far more reasonable amounts.

这篇关于WP8内存泄露开幕式和闭幕式的PhoneApplicationPage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆