3

I know very similar questions were asked here in the past - but neither had a solution for my problem:

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;
    }

And then use this (with INotifyPropertyChange) to bind the resulting BitmapImage to the Source of a Image object (on a Page).

Problem is: this will leak memory (a lot in my case up to 300MB on 2-3 images!)

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): Memory-Profiler output

enter image description here

No matter what I try - I don't get this leak away. Tried (single and all at once):

  • clear the Visual-Tree / remove the Image on Unload
  • Set the Image-Source to null
  • use ImageBrush in Rectangle instead of Image
  • other CacheOptions without Disposing the MemoryStream
  • don't Freeze the Image

I don't get this - really! As soon as I stop using the Image in the Source everything is ok (no leak).

Someone any options I can try?

REMARK Seems 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

Random Dev
  • 51,810
  • 9
  • 92
  • 119
  • hmm... don't know if this is *really* a memory-leak as the "leak" seems to be bounded at about 550MB - after this some (200-300MB) memory is freed up if I once again load a bitmap - but still this is not a desireable behavior at all... – Random Dev Feb 07 '12 at 12:32
  • 2
    well ... oh my - seems like the problem is caused by stupid picture use. The pictures are provided by the user (some kind of maps - black/white with lots of white) and I only looked at the file-sizes (around 1MB) - BUT those are .JPG and the pictures have a resolution of up to 10,000 X 5,000 pixels(sic!) - so unpack this into 32bit and you easily get to >150MB per Picture ... so I guess I can call this one "solved" but I will let it open for now - maybe there are some good comments and I still have to try the "workaround" (= resonable pictures) – Random Dev Feb 07 '12 at 12:54

1 Answers1

0

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...

Random Dev
  • 51,810
  • 9
  • 92
  • 119