Notice:
Memory not getting released in WPF Image
I checked above post, in that post, the Image is associate with a BitmapImage
. But for my case, I use XamlAnimatedGif
package. They are different. And I verified his solution doesn't work for my issue.
Question:
In my WPF application, I use XamlAnimatedGit
package to play a gif as loading animation. And after playing, I want to remove it and release its memory occupation.
My code is as below.
When app starts, I will call ShowLoading()
, and after e.g. 5s, I will call HideLoading()
.
But if I don't add the loading gif at all, then my application will consume about 40MB memory. While by adding and playing the loading gif, my application will consume about 100MB memory, and will not release the memory even after HideLoading()
is called.
LoadingBorder.Child = null;
in HieLoading()
causes CPU usage dropped from 2.3% to 0.1%, but it doesn't reduce the memory usage.
How to release the memory usage occupied by the gif Image?
xaml code:
<Border x:Name="LoadingBorder" Visibility="Hidden" Width="250" Height="250">
<Image Name="LoadingImageUI" Stretch="UniformToFill"/>
</Border>
cs code:
private void HideLoading()
{
if (LoadingBorder.Visibility == Visibility.Hidden)
return;
LoadingBorder.Height = 0;
LoadingBorder.Margin = new Thickness(0, 0, 0, 0);
LoadingBorder.Visibility = Visibility.Hidden;
LoadingBorder.Child = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
private void ShowLoading()
{
LoadingBorder.Height = 250;
LoadingBorder.Visibility = Visibility.Visible;
LoadingUri = new Uri("Asset/pictures/Loading_Dark.gif", UriKind.Relative);
AnimationBehavior.SetSourceUri(LoadingImageUI, LoadingUri);
}
Update:
I add GC.Collect()
in HideLoading()
, but the memory still cannot be released.