0

This is a duplicate of WPF severe Image memory leak, however, that one got closed for being a duplicate. The duplicate question that question got referred to doesn't answer my question, as it just says it's normal and will be picked up by GC.

However in this case scenario my app will be run in low-resource situations, so it's imperative that the Image is released. Is there any ways to forcefully release the memory used by the Image? Here is the code (Note, this is THE ENTIRE CODE, no other references)

public Window1()
{
    InitializeComponent();
    BitmapImage bi = new BitmapImage();
    bi.BeginInit();
    bi.CacheOption = BitmapCacheOption.OnLoad;
    bi.UriSource = new Uri(@"C:\users\bb\pictures\wallpaper.png");
    bi.EndInit();
    bi.Freeze();
    Test.Source = bi;
}

internal void Delete()
{
    Test.Source = null;
    Test.UpdateLayout();
    Root.Children.Remove(Test);
}

private Window1 window1;
public MainWindow()
{
    InitializeComponent();
    window1 = new Window1();
    window1.Show();
}

private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
    window1.Delete();
    window1.Close();
    window1 = null;

    GC.AddMemoryPressure(1000000000);

    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.WaitForFullGCComplete();
    GC.Collect();
    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.WaitForFullGCComplete();
    GC.Collect();
    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.WaitForFullGCComplete();
    GC.Collect();
    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.WaitForFullGCComplete();
    GC.Collect(); //Doesn't help
}

I have also used DotMemory to force a GC numerous times, however, 190MB unmanaged memory still remains. This is just like the accepted answer WPF Image control not disposing source, however that question does not answer how to release the memory.

  • In order to free the object's memory, you must ensure that there is no reference to `Test.Source`. If this is a public property and external code gets a copy of the reference and e.g. stores it as instance variable, then the object can't be released by `Window1`. You can use the memory profiler to take a snapshot of the current memory to investigate the active object instances. This way you can check if the unmanaged memory is consumed by the image or something else. It is not clear if you have already checked what exact object(s) allocate the 190MB. – BionicCode Aug 24 '20 at 09:17
  • @BionicCode There is no other references to Test.Source. This is a test/sample project I created. – User12341921 Aug 24 '20 at 17:38
  • Check this thread, I've run into similar issues before and using StreamSource rather than UriSource usually did the trick (I defaulted to this for a long time): https://social.msdn.microsoft.com/Forums/vstudio/en-US/dee7cb68-aca3-402b-b159-2de933f933f1/disposing-a-wpf-image-or-bitmapimage-so-the-source-picture-file-can-be-modified?forum=wpf – Alexandru Clonțea Aug 25 '20 at 17:00

0 Answers0