I am making a video game. At this level, I need to find a way to release memory from images, once I set their source to null.
When I am not using an image, its source is set to null.
imgExample.Source = null;
But it doesn't free enough memory.
When I did similar thing to MediaElements, I used this approach to free unused memory:
foreach (MediaElement thisMedia in Videos)
{
thisMedia.Source = null;
thisMedia.Stop();
thisMedia.Close();
}
Then applied:
GC.Collect();
at relevant place.
Now, I am trying to do something similar with Images. The "Close" can't be used on Images. I tried using "Freeze()", but it only made worse. In any case, Freeze is used when assigning new source to Image, and I want the opposite - to do something when source becomes null.
I tried googling on the issue, and got to this page: https://msdn.microsoft.com/en-us/library/system.drawing.image(v=vs.110).aspx
I thought that methods: "Dispose" and "Finalize" can be useful.
However, I can't just use:
imgExample.Dispose();
or
imgExample.Finalize();
Either the approach is incorrect, or I am missing a using directive.
What is the correct way to dispose of unused memory?
EDIT: I forgot to say, that "imgExample" is an image defined in XAML. Preferably NOT to replace such images with new ones in code behind.