title: WPF ViewModel GC
body: <p>I have a ViewModel that on receiving an event from a Model, shows a dialog to the user by newing up the dialog's ViewModel and passing it to the dialog for data binding, i.e.,</p>
  
  <pre><code>public class MainViewModel
  {
      ...
      private void OnModelRaisedEvent(object sender, EventArgs e)
      {
          DialogViewModel dialogViewModel = new DialogViewModel();
          Window dialog = new DialogView(dialogViewModel);
          dialog.ShowDialog();
      }
   }
  </code></pre>
  
  <p>In the dialog's view I hook into a button click to close the window, i.e., </p>
  
  <pre><code>public class DialogView : Window
  {
      public DialogView(DialogViewModel viewModel)
      {
          InitializeComponent();
          Loaded += (s,e) =&gt; {DataContext = viewModel; };
      }
  
      ...
  
      private void ButtonOnClick(object sender, RoutedEventArgs e)
      {
          Close();
      }
   }
  </code></pre>
  
  <p>Since only the DialogView is using the DialogViewModel, can I be confident that I won't get a memory leak here? </p>
  
  <p>For example, if I open and close the dialog multiple times, will the DialogViewModel get GC'd when the DialogView is closed so not to accumulate multiple instances of the DialogViewModel. Watched memory usage in task manager, and it does go up when opening and closing DialogView multiple times, but not sure if this is because the GC just hasn't kicked in.</p>