.net4中的内存泄漏 - 将内存BitmapImage绑定到Image-Source

时间:2012-02-07 11:51:21

标签: wpf memory-leaks bitmapimage imagesource

我知道过去在这里提出了非常类似的问题 - 但是我的问题都没有解决方案:

我将内存中的图像加载到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对象的Source(在页面上)。

问题是:这会泄漏内存(在我的情况下很多,2-3张图片上的300MB!)

你甚至没有使用Profilers找到它 - 只有.net Memory Profiler让我走上正轨(因为它在非托管内存中所有字节都去了 - 所以ANTS告诉我“.NET正在使用19,24MB的367,分配给应用程序的3MB总私有字节“ - 很好”: Memory-Profiler output

enter image description here

无论我尝试什么 - 我都不会泄漏。 尝试过(单一和全部):

  • 清除可视树/删除卸载时的图像
  • 将Image-Source设置为null
  • 在Rectangle中使用ImageBrush而不是Image
  • 其他没有释放MemoryStream的CacheOptions
  • 不要冻结图像

我不明白 - 真的! 一旦我停止使用源中的图像一切正常(没有泄漏)。

我可以尝试任何选项吗?

REMARK 看起来这根本没有错误(请参阅我的第二条评论) - 我必须检查这一点,所以我现在暂时打开这个问题 - 也许这可以帮助解决其他问题

1 个答案:

答案 0 :(得分:0)

对不起伙计们 - 这确实不是“BUG”,而是由高分辨率图片引起的。

如果我应该删除这个问题,或者我应该把它留在这里,因为其他人可能会遇到同样的情况,请对此发表评论......

相关问题