Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Let's create a:
C#
ObservableCollection<BitmapImage> BitmapsLists = new ObservableCollection<BitmapImage>();

Then let's fill it with some random Images:
C#
foreach (string item in Directory.GetFiles("Some Folder"))
{
    BitmapImage BitImage = new BitmapImage();
    BitImage.BeginInit();
    BitImage.CacheOption = BitmapCacheOption.None;
    BitImage.UriSource = new Uri(item);
    BitImage.EndInit();
    BitImage.Freeze();
    BitmapsLists.Add(BitImage);
}

Checking the memory consuption till this we will notice hardly 10Mb, maybe less..
(I am currently dealing with 50 Jpgs around 500Kb each).

After this let's link out Bitmaps with some ImageSources of Image(controls) on a Canvas.

C#
foreach (BitmapImage item in BitmapsLists)
{
    Image myImage = new Image();
    myImage.source = item;
    // Then add it to Canvas Children setting Left and Top Property and calling the add function.
}

You will notice that when this finishes we will have around 500Mb of memory consumption.

Fine..

Now let's clear all of our ImageSources:
C#
foreach (Image item in Canvas.Children)
{
    item.source = null;
}

Good..
Now let's clear our Children and force a ReRendering with
--> InvalidateVisual(); // Not Needed as i recall..
C#
Canvas.Children.Clear;
InvalidateVisual();

Ok now why i still have 500MB of RAM still Being occupied..?

I have no more References and the BitmapImages are only laying into the Collection..
Nothing is being drawed...!

*Code on codeblocks may have some mistakes no compiler on this pc..

/// Update 1:

Just tryed also the StreamSource after reading this : http://stackoverflow.com/questions/1684489/how-do-you-make-sure-wpf-releases-large-bitmapsource-from-memory[^]

Still no luck..!
Posted
Updated 23-Nov-11 8:08am
v3

I found it..
I'll post the solution l8
---------------------------------

Just Because you create a BitmapImage using a Uri as source doesn't mean that it it being created at the same time even if you set the BitmapCacheOption to whatever value..

The BitmapImage is gonna get Loaded into memory when you use it as Source in a Control like Image.

Additionally the only way i've found to load the Bitmap instantly is to set its StreamSource to a Stream BUT be carefull to also set the "source.CacheOption = BitmapCacheOption.OnLoad;"

The example is this:

C#
var source = new BitmapImage();
using(Stream stream = ...)
{
  source.BeginInit();
  source.StreamSource = stream;
  source.CacheOption = BitmapCacheOption.OnLoad;
  source.EndInit();
}


And the credits go to Ray Burns
from http://stackoverflow.com/questions/1684489/how-do-you-make-sure-wpf-releases-large-bitmapsource-from-memory[^].
 
Share this answer
 
v2
Comments
LanFanNinja 23-Nov-11 14:48pm    
That's great! So what was the problem?
Try this

C#
GC.Collect();


GC.Collect Method [^]
 
Share this answer
 
Comments
Freeboss 23-Nov-11 13:24pm    
I'd really wish that was so simple.. :p
LanFanNinja 23-Nov-11 13:52pm    
Tried that already huh? :) Well I wish I could be of more assistance but I simply do not know. Automatic garbage collection is both a blessing and a curse.
Freeboss 23-Nov-11 14:00pm    
Thanks for your effort LanFanNinja.

I've updated the Question since one more approach just died..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900