19

First, I load a BitmapImage into the Image control, whice is located on the Window. Then I work with the Image control and then close the Window.

I do it 2-3 times in a minute and my memory fills up very quickly because the images do not unload from the memory for some reason when the window is closed.

So how do I unload BitmapImage from Image.Source control manually to free the RAM?

AgentFire
  • 8,944
  • 8
  • 43
  • 90

4 Answers4

32

I believe the solution you are looking for is at http://www.ridgesolutions.ie/index.php/2012/02/03/net-wpf-bitmapimage-file-locking/. In my case, I was trying to find a way to delete the file after it was created, but it appears to be a solution to both issues.

Doesn't free up memory:

var bitmap = new BitmapImage(new Uri(imageFilePath));

Frees up memory, and allows file to be deleted:

var bitmap = new BitmapImage(); 
var stream = File.OpenRead(imageFilePath);

bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.StreamSource = stream;
bitmap.EndInit();
stream.Close();
stream.Dispose();

Optionally, also freeze the BitmapImage:

bitmap.Freeze();
majestzim
  • 518
  • 6
  • 16
  • Works for me, change bitmap.CacheOption further reduce the commited memory. BTW, stream.Close and stream.Dispose are duplicated. – zhaorufei Apr 13 '18 at 02:34
  • Please note: I recall originally attempting to put the stream in a using statement, but memory continued to build up very quickly. Using stream.close and stream.dispose worked much better. – majestzim Aug 16 '18 at 15:09
3

In my situation it seems that the bitmap caching was the issue. I was previously loading bitmaps like this:

Bitmap bitmap = new Bitmap();

using(var stream = new FileStream(...))
{
    bitmap.BeginInit();
    bitmap.CacheOption = BitmapCacheOption.OnLoad;
    bitmap.StreamSource = stream;
    bitmap.EndInit();
}

bitmap.Freeze();
image.Source = bitmap;

Continuously replacing image.Source the same way just built up memory, manually forcing garbage collection wasn't really helping.

Instead, disabling the caching and having it use the stream (requires leaving the stream open until the image is displayed) paired with manual garbage collection eliminated memory build up for me.

Stream mediaStream;

void DisposeMediaStream()
{
    if (mediaStream != null)
    {
        mediaStream.Close();
        mediaStream.Dispose();
        mediaStream = null;
        GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true);
    }
}

void Update()
{
    DisposeMediaStream();

    var bitmap = new BitmapImage();
    mediaStream = new FileStream(...);

    bitmap.BeginInit();
    bitmap.CacheOption = BitmapCacheOption.None;
    bitmap.StreamSource = mediaStream;
    bitmap.EndInit();

    bitmap.Freeze();
    ControlImage.Source = bitmap;
}

This way I can cycle through tons of images (like Windows Photo Viewer) and memory stays low. Note that the stream does not have to stay open once the image has actually rendered.

sawch
  • 215
  • 1
  • 2
  • 8
  • I tried the above code, but had to set BitmapCacheOption to OnLoad or else I cannot run Update() to add multiple images in a loop – KMC Mar 27 '18 at 02:52
  • When you set a BitmapImage's StreamSource, always also set `BitmapCacheOption.OnLoad` and close the stream right after EndInit, like `using (var fs = new FileStream(...)) { ... bitmap.CacheOption = BitmapCacheOption.OnLoad; bitmap.StreamSource = fs; bitmap.EndInit(); }` – Clemens Aug 24 '20 at 09:39
0

You can set the object to null, so that the BitmapImage object is no longer referenced. In this situation, the GC should take care of freeing up resources. You can call GC.Collect but it can affect performance if used too often.

abhinav
  • 3,199
  • 2
  • 21
  • 25
  • `GC.Collect()` does not free the resources even when i null the reference to the `BitmapImage`. – AgentFire Dec 02 '11 at 07:42
  • How does your reference chain look? Is it something like `Image->BitmapImage->MemoryStream->filestream->Actual bytes in memory` then you'll have to null-reference the correct object, else you'll still be using memory. – abhinav Dec 02 '11 at 08:02
  • It looks like this: `Image.Source = new BitmapImage(new Uri(link));` – AgentFire Dec 02 '11 at 08:11
  • Seems to be I need to use Image.BeginInit and .EndInit. – AgentFire Jan 11 '12 at 07:49
-1

You can call Dispose() on the images in the window's Closed event. I think it may also be possible to optimise your memory footprint using different caching options.

Edit:

You can't call Dispose(), instead, you might consider BitmapCacheOption.None. The image will be read directly from disk and not cached in memory.

Minustar
  • 1,185
  • 1
  • 10
  • 23
  • I've tried to use `new BitmapImage(new Uri(link), new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore));` but there is no difference. Memory is still in use. – AgentFire Dec 02 '11 at 07:46