我创建了一个动态下载并保存多个大图像的数据透视表。 我首先使用webclient下载图像并将其写入磁盘。 比创建一个包含所有图像的图像列表数组。 基本思想是我只在需要时才加载图像。 说我的清单中有12张图像。每张幻灯片只显示1个图像。假设我们正在查看图片7.我继续为下一张幻灯片预加载图片6 & 8。OutOfMemory异常<BitmapImage> .SetSource()
1, 2, 3, 4, 5, [6, {7}, 8], 9, 10, 11, 12
当用户通过幻灯片浏览时,我保持图像在“[]”之外预加载并卸载。
我用于预加载图像的代码:
BitmapImage bi = new BitmapImage();
bi.SetSource(GetStream(fileName);
IMGSource = bi; // IMGSource<ImageSource> referenced by the xaml in Image Binding IMGSource.
我用来卸载图像的代码:
IMGSource = null;
GC.Collect(); // I force the program to Garbage collect since the image is really large.
的问题是,一些图像被观看后(约9图像)。它会在该行中引发OutOfMemory异常:bi.SetSource
。
我检查了卸载功能都工作正常(它不释放内存调用收集后,和内存保持在一个稳定的点)
但它仍然抛出一个异常OutOfMemory
。 我该怎么办?
编辑:
我才发现,其实内存通过幻灯片导航时继续上升
。通过调用:
Microsoft.Phone.Info.DeviceStatus.ApplicationCurrentMemoryUsage;
它给喜欢回忆:
54.7 MB, 76.91 MB, 111.94 MB, 105 MB, 112.71 MB, 141.93 MB, 148.42 MB, Exception thrown
但通过调用:
GC.GetTotalMemory(false);
它显示只有约1.2~1.3 MB
。
发生了什么?不应该释放内存?
请对[格式](http://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks)更敏感 – 2013-02-16 18:26:24
对不起,我正在使用我的手机发布。它没有预览给我,我输入了四个空格代码。但事实证明它并不起作用:\ – user1510539 2013-02-16 18:29:28
这可能不是泄漏的根源,但您应该确保在加载图片后关闭流:'使用(var stream = GetStream(fileName)){bi .SetSource(流); }' – 2013-02-16 19:20:48