title: still memory-leaks in .net4 - binding memory BitmapImage to Image-Source
body: <p>I know very similar questions were asked here in the past - but neither had a solution for my problem:</p>
  
  <p>I load a image from memory into a BitmapImage:</p>
  
  <pre><code>    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;
      }
  </code></pre>
  
  <p>And then use this (with INotifyPropertyChange) to bind the resulting BitmapImage to the Source of a Image object (on a Page).</p>
  
  <p>Problem is: this will leak memory (a lot in my case up to 300MB on 2-3 images!)</p>
  
  <p>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):
  <img src="https://i.stack.imgur.com/BBJxx.jpg" alt="Memory-Profiler output"></p>
  
  <p><img src="https://i.stack.imgur.com/cOVWe.jpg" alt="enter image description here"></p>
  
  <p>No matter what I try - I don't get this leak away.
  Tried (single and all at once):</p>
  
  <ul>
  <li>clear the Visual-Tree / remove the Image on Unload</li>
  <li>Set the Image-Source to null</li>
  <li>use ImageBrush in Rectangle instead of Image</li>
  <li>other CacheOptions without Disposing the MemoryStream</li>
  <li>don't Freeze the Image</li>
  </ul>
  
  <p>I don't get this - really!
  As soon as I stop using the Image in the Source everything is ok (no leak).</p>
  
  <p>Someone any options I can try?</p>
  
  <p><strong>REMARK</strong>
  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</p>