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 ().
Eamon nerbonne
source share