0

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.

Tom Xue
  • 3,169
  • 7
  • 40
  • 77
  • @Clemens I tried GC.Collect(), but still cann't release the memory. – Tom Xue Apr 19 '22 at 10:00
  • @Clemens Which code? GC.Collect()? I tried it but found that it is useless to release memory, so I delete the line. – Tom Xue Apr 19 '22 at 10:02
  • I add it now, please check the main post. @Clemens – Tom Xue Apr 19 '22 at 10:06
  • You can't explicitly "release the memory" in a managed app. You can force the garbage collector to collect unreachable objects using the `GC.Collect` method but this doesn't necessarily release the memory back to the operating system immediately. – mm8 Apr 19 '22 at 12:45

0 Answers0