0

I want to close a windows which is currently shown. But when I call Close(), it only hide the window. And the memory length still grows instead of decreasing.

I also use this answer to fix this problem:

https://stackoverflow.com/a/34651426/9135351

but it doesn't work.

Here is my code to show a new Window and close the shown Window

C#

private void Home_Click(object sender, RoutedEventArgs e)
{
   new HomeContentView().Show(); //Open Home window
   Close(); //Close current window
   GC.Collect(); //Realease
}

Xaml

 <Button
     ToolTip="Home"
     Click="Home_Click"
     Cursor="Hand"
     Background="Transparent"
     BorderBrush="Transparent"
     Margin="0,0,221,0"
     HorizontalAlignment="Right"
     Width="48" Height="48"
     VerticalAlignment="Top">

     <materialDesign:PackIcon Kind="Home" Width="37" Height="38" 
     Foreground="Black" HorizontalAlignment="Stretch" />
 </Button>
  • Can you show us your code? It's a bit impossible to guess what you have already done.. – Mikev Feb 27 '19 at 11:00
  • 1
    Did you read https://stackoverflow.com/questions/50894863/wpf-increase-memory-each-time-open-and-close-window ? – Djordje Nedovic Feb 27 '19 at 11:06
  • Here is the code to open new Window and close the current \n `new HomeContentView().Show();` `Application.Current.Windows[1].Close();` –  Feb 27 '19 at 11:06
  • No I didn't. I just saw. I will try and keep you informed –  Feb 27 '19 at 11:07
  • @DjordjeNedovic. I try to use `GC.collect`, but it doesn't work correctly. The memory is always growing –  Feb 27 '19 at 11:28
  • @2kFerd, can you please update your question by adding code? Maybe something else is responsible for memory growing – Djordje Nedovic Feb 27 '19 at 11:35
  • I do it. I add the both c# and xaml code –  Feb 27 '19 at 11:48
  • @2kFerd does 'HomeContentView' subscribe to any events on singletons or the like? If so those connections keep the view alive since there is still an active reference from the root to that object. – Johannes Feb 27 '19 at 11:57
  • @Johannes, I don't get you. Could you explain your answer, please –  Feb 27 '19 at 12:23
  • When an observer subscribes to an event the subject has to keep a reference in order to notify. That reference keeps the observer alive. So if you have an event subscription in your 'HomeContentView' constructor it would not matter that you closed it it would stay alive and never be collected. WPF internally handles all it subscriptions correctly - when you implement them you need to watch out for that. I mention it because you are looking for a memory leak in c#. – Johannes Feb 27 '19 at 12:29
  • Thanks @Johannes. I wonder if `static` field can keep the view alive too? –  Feb 27 '19 at 14:20
  • @2kFerd a static field should not keep any of the instances alive unless it tracks the instances like a static list of instances for example. it comes down to weather or not you can use the static field to navigate to an instance or not. A static integer would not be problematic. – Johannes Feb 27 '19 at 14:24

2 Answers2

1

Close() should do the trick. Why can't you use the object created for the Window and Close that instead of accessing it from the Application.Current.Windows

var window = new HomeContentView(); 
window.Show(); // Shows window
window.Close(); // Closes window
dhilmathy
  • 2,800
  • 2
  • 21
  • 29
  • The both do the same thing. Each time I use `Close()` or `Application.Current.Windows`, the visible window is closed but the memory grows until 433M. That's my problem. –  Feb 27 '19 at 11:25
  • 1
    If so, i believe there might be memory leak from some where else. I suggest you to use some profiling tools to understand better on this. – dhilmathy Feb 27 '19 at 11:28
  • Ok! Thanks. I will search again –  Feb 27 '19 at 11:34
0
public void MyMethod()
    {
        var myWindow = this;
        var control = myWindow.Content;
        //do stuff
        control = null; // is that okay??
        myWindow.Close();
    }