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.