Memory not being relieved when setting BitmapImage to null(将 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 时未释放内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 BitmapImage 设置为 null 时未释放内存


- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 输入按键事件处理程序 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04