I am using a media element to display several kinds of resources (image, gif, video)
The issue is that the media element will consume more memory each time I assign it a new source. The garbage collector is not collecting anything there. I have found several questions about the same topic in forums and on stack overflow but none of them received a proper answer.
So this my wpf code:
private void LoadInformation(FileInfo file)
{
imageDisplay.Source = new Uri(file.FullName);
}
<MediaElement x:Name="imageDisplay" Grid.Column="0" Grid.Row="0" UnloadedBehavior="Close" LoadedBehavior="Manual"
MediaOpened="Element_MediaOpened" MediaEnded="Element_MediaEnded" ></MediaElement>
this is my code to update in .xaml.xs:
// When the media opens, initialize the "Seek To" slider maximum value
// to the total number of miliseconds in the length of the media clip.
private void Element_MediaOpened(object sender, EventArgs e)
{
imageDisplay.Play();
}
// When the media playback is finished. Stop() the media to seek to media start.
private void Element_MediaEnded(object sender, EventArgs e)
{
imageDisplay.Stop();
imageDisplay.Source = null;
}
This is according to official microsoft documentation: https://learn.microsoft.com/en-us/dotnet/desktop/wpf/graphics-multimedia/how-to-control-a-mediaelement-play-pause-stop-volume-and-speed?view=netframeworkdesktop-4.8
if I execute LoadInformation in a loop, my memory goes up happily, until the application crashes.
If I stop the loop before the app crashes and let it run for a while, gc will not free up the memory

Update 1
Managing the MediaElement Manually does not make any difference: LoadedBehavior = MediaState.Manual;
imageDisplay.Stop();
imageDisplay.Close();
imageDisplay.Source = null;
imageDisplay.Source = new Uri(file.FullName);
imageDisplay.Play();


imageDisplay.Close()in yourElement_MediaEndedmethod? I knowUnloadedBehavior="Close"should do it automatically but it might force it to release the memory