[英]How to free image cache/memory in Windows Phone 8?
在我的Windows Phone 8应用程序中,我有一个带有LongListSelector
的页面,该页面绑定到1000个对象的列表,这些对象具有base64string
的属性。 现在要显示图像,我编写了此转换器以将bas64string
转换为stream
。
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (!value.ToString().Contains("http://"))
{
string str = value.ToString();
byte[] bytes = Converter.FromBase64String(str);
using (MemoryStream stream = new MemoryStream(bytes))
{
stream.Seek(0, SeekOrigin.Begin);
BitmapImage image = new BitmapImage();
image.SetSource(stream);
bytes = null;
var memoryusage = string.Format("Memory: {0} bytes",
DeviceExtendedProperties.GetValue("ApplicationCurrentMemoryUsage"));
Debug.WriteLine(memoryusage);
return image;
}
}
else
{
return null;
}
}
这是记忆的用法:
Memory: 92549120 bytes Memory: 92946432 bytes Memory: 92946432 bytes Memory: 92946432 bytes Memory: 92946432 bytes Memory: 93192192 bytes Memory: 93192192 bytes Memory: 96079872 bytes Memory: 100700160 bytes Memory: 100700160 bytes Memory: 109568000 bytes Memory: 111734784 bytes Memory: 142852096 bytes Memory: 143056896 bytes Memory: 143056896 bytes Memory: 143261696 bytes Memory: 140791808 bytes Memory: 141103104 bytes Memory: 141529088 bytes Memory: 142151680 bytes Memory: 146784256 bytes Memory: 146784256 bytes Memory: 155066368 bytes Memory: 156368896 bytes
当内存等于或可能大于此156368896字节的某个字节时,应用程序将由于EngineExecutionException
崩溃。 一旦我得到“ OutOfMemoryException”:
image.SetSource(stream);
显然,这是一个内存问题。 我需要清除图像缓存,但是如何清除? 我在此答案中看到了链接https://stackoverflow.com/a/12267163/1949475 ,但是我无法使用它。
注意:并非所有图像都同时显示,在我返回并再次返回页面后,应用程序会占用大量内存,从而更改了要在LongListSelector中显示的数据。
在转换器类中进行设置很重要
BitmapImage image = new BitmapImage();
image.DecodePixelType = DecodePixelType.Logical;
image.CreateOptions = BitmapCreateOptions.BackgroundCreation;
image.CreateOptions = BitmapCreateOptions.DelayCreation;
image.DecodePixelWidth = 56;
image.DecodePixelHeight = 100;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.