WPF WriteableBitmap Memory Leak?
- by Mario
Hello, everyone!
I'm trying to figure out how to release a WriteableBitmap memory.
In the next section of code I fill the backbuffer of a WriteableBitmap with a really large amount of data from "BigImage" (3600 * 4800 px, just for testing)
If I comment the lines where bitmap and image are equaled to null, the memory it´s not release and the application consumes ~230 MB, even
when Image and bitmap are no longer used!
As you can see at the end of the code its necessary to call GC.Collect() to release the memory.
So the question is, what is the right way to free the memory used by a WriteableBitmap object? Is GC.Collect() the only way?
Any help would be great.
PS. Sorry for my bad english.
private void buttonTest_Click(object sender, RoutedEventArgs e)
{
Image image = new Image();
image.Source = new BitmapImage(new Uri("BigImage"));
WriteableBitmap bitmap = new WriteableBitmap(
(BitmapSource)image.Source);
bitmap.Lock();
// Bitmap processing
bitmap.Unlock();
image = null;
bitmap = null;
GC.Collect();
}