将 BitmapImage 设置为 null 时未释放内存 [英] Memory not being relieved when setting BitmapImage to null

查看:28
本文介绍了将 BitmapImage 设置为 null 时未释放内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象集合,每个对象都包含一个 BitmapImage.我将此集合绑定到 FlipView.当用户在 FlipView 中翻页时,所选对象的 BitmapInmage 从 ApplicationData 加载,我将前一个对象的 BitmapImage 设置为 null 以释放内存.问题是,内存永远不会被释放,并且应用程序在翻转一段时间后崩溃.当我查看集合时,只有实际项目设置了 BitmapImage,所有其他项目都将其设置为 null.那么如何解除记忆呢?

I have a collections of objects, each object contains a BitmapImage. I have this collection bound to a FlipView. When user flips a page in he FlipView, the BitmapInmage of the selected object gets loaded from ApplicationData and I set the BitmapImage of the previous object to null to relieve the memory. The problem is, that the memory never gets relieved and the app crashes after some time of flipping. When I look at the collection, only the actual item has its BitmapImage set, all the others have it as null. So how do I relieve the memory?

我加载图片的方式:

StorageFile s = await ApplicationData.Current.LocalFolder.GetFileAsync(localFilename);
BitmapImage bitmapImage = new BitmapImage();
using (var stream = await s.OpenAsync(FileAccessMode.Read))
{                    
    bitmapImage.SetSource(stream);
}                
return bitmapImage;

我认为问题可能是我加载图像的方式,我猜文件流没有被释放

I think the problem may be the way I load the Image, I guess the file stream does not get freed

推荐答案

我在 Windows Phone 7 应用程序中遇到了类似的问题.

I ran into a similar issue with Windows Phone 7 applications.

对我有用的一个技巧是从父元素中删除图像.

One trick that worked for me was removing the image from the parent element.

grid1.Children.Remove(image1);
image1 = null;

我不确定这是否对您的情况有所帮助.

I'm not sure if this will help in your situation though.

我的 博文.

这篇关于将 BitmapImage 设置为 null 时未释放内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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