将 BitmapImage 设置为 null 时未释放内存

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)