Hi,
Let's create a:
ObservableCollection<BitmapImage> BitmapsLists = new ObservableCollection<BitmapImage>();
Then let's fill it with some random Images:
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.
foreach (BitmapImage item in BitmapsLists)
{
Image myImage = new Image();
myImage.source = item;
}
You will notice that when this finishes we will have around 500Mb of memory consumption.
Fine..
Now let's clear all of our ImageSources:
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..
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..!