WPF WriteableBitmap memory leak?

I am trying to figure out how to free up WriteableBitmap memory.

In the next section of code, I fill the WriteableBitmap buffer buffer with a really large amount of data from "BigImage" (3600 * 4800 px, for testing only) If I comment on lines where the bitmap and image are zero, the memory is not freed and the application consumes ~ 230 MB, even when the image and bitmap are no longer used!

As you can see at the end of the code, you need to call GC.Collect () to free memory.

So the question is, what is the right way to free the memory used by the WriteableBitmap? 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(); } 
+6
memory-leaks wpf writeablebitmap
source share
3 answers

Do you run your test on Windows XP using .Net 3.5 SP1? If so, then this is a known issue that will be fixed in 4.0.

See http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5d88cdf1-e992-4ad4-8f56-b5dbf92dcf1c

+3
source share

In the general case, memory should ultimately be automatically freed up as needed.

However, for this to happen, you must be sure that the object is not really used: links to the object cannot exist anywhere, including links that are "no longer used." Therefore, in particular, if you put your WriteableBitmap and the original BitmapSource into variables of a long-lived class, they will not be released until the container is empty.

In addition, WPF uses the saved GFX model: when rendering, you actually save the rendering instructions. The β€œinstructions” on how to render a bitmap include a link to the bitmap, so if you have ever rendered a large bitmap, then for a while (at least as long as it is on the screen - even if the version on the screen is tiny ) those images will be saved.

On practice; store links to these bitmaps only where they are needed, and if the context in which they live is long-lived (a long method call or a method call creating a closure with a link to the bitmaps, or a member of a long-living class), then set they are null if they are no longer needed.

No need to manually free memory; GC.Collect () should be redundant. As a rule, use GC.Collect only during benchmarking to get an indication of memory consumption and / or start from scratch. Overall performance tends to decrease on calls. GC.Collect ().

+1
source share

Forcing the GC without setting image and bitmap to null will not clear them, because they are still referenced locally and therefore considered root links. This has nothing to do with WriteableBitmap specifically and more a question of how the GC works.

If you do not set them to null and do not force garbage collection, they are collected after the method exists and GC occurs. This is recommended above, forcing the collection on its own, because you are likely to hurt performance rather than helping it.

0
source share

All Articles