【发布时间】: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));
}
【问题讨论】:
-
你试过在
GC.Collect();里面使用OnBackKeyPress吗?
标签: c# memory-leaks windows-phone-8 out-of-memory longlistselector