问题描述
我知道过去曾在这里问过非常类似的问题-但都没有解决我问题的方法:
I know very similar questions were asked here in the past - but neither had a solution for my problem:
我将内存中的图像加载到BitmapImage中:
I load a image from memory into a BitmapImage:
private static BitmapImage LoadImage(byte[] imageData)
{
if (imageData == null || imageData.Length == 0) return null;
var image = new BitmapImage();
using (var mem = new MemoryStream(imageData))
{
mem.Position = 0;
image.BeginInit();
image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = null;
image.StreamSource = mem;
image.EndInit();
}
image.Freeze();
return image;
}
然后将其(与INotifyPropertyChange一起使用)将生成的BitmapImage绑定到Image对象的源(在页面上).
And then use this (with INotifyPropertyChange) to bind the resulting BitmapImage to the Source of a Image object (on a Page).
问题是:这将泄漏内存(在我的情况下,在2-3张图像上最多可能浪费300MB!)
Problem is: this will leak memory (a lot in my case up to 300MB on 2-3 images!)
您甚至没有使用Profilers来找到它-只有.net Memory Profiler使我步入正轨(因为它位于所有字节都在非托管内存中,所以ANTS告诉我".NET正在使用19,24MB的367,分配给应用程序的专用字节总数为3MB"-不错):
You don't even find this using Profilers - only the .net Memory Profiler got me on track (as it's in unmanaged memory where all the bytes go - so ANTS tell me ".NET is using 19,24MB of 367,3MB total private bytes allocated to the application" - nice):
无论我如何尝试-我都无法避免泄漏.尝试过(一次又一次):
No matter what I try - I don't get this leak away.Tried (single and all at once):
- 清除视觉树/卸载时删除图像
- 将图像源设置为空
- 在矩形中使用ImageBrush而不是Image
- 其他不处理MemoryStream的CacheOptions
- 不要冻结图像
我不明白这个-真的!一旦我停止在Source中使用Image,一切正常(没有泄漏).
I don't get this - really!As soon as I stop using the Image in the Source everything is ok (no leak).
有人可以尝试吗?
备注看来这根本没有错误(请参阅我的第二条评论)-我必须检查一下,以便现在让我打开问题-也许这也可以帮助解决其他问题
REMARKSeems like this is no bug at all (see my second comment) - I have to check this so I will let the question open for now - maybe this can help with the other questions on this as well
推荐答案
对不起-这的确不是错误",而是由高分辨率图片引起的.
Sorry guys - this was indeed no "BUG" but caused by high-resolution pictures.
如果我要删除问题,还是应该将其留在这里,因为其他人可能会遇到同样的情况,请对此发表评论...
Please comment on this if I should delete the question or if I should leave it here as other people might come into the same situation...
这篇关于.net4中仍然存在内存泄漏-将内存BitmapImage绑定到图像源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!