I am working on a WPF application which working with very large binary datasets. However we are having huge memory leaks. It appears the garbage collector is never reclaiming the space and the Window remains in memory forever. This is leading to OOM issues.
Here is a snippet of code from our Login window which launches the MainWindow upon successful authentication.
I am explicitly setting mainWindow reference to null. However using dot Memory I can see there are multiple version in memory when we log out and log back in? This is happening all over the application, this is just one example.
var mainWindow = new MainWindow();
Hide();
mainWindow.ShowDialog();
if (mainWindow.LogoutOnClose)
{
mainWindow.LogoutOnClose = false;
OmegaApp.Instance.LogOff();
mainWindow.Close();
mainWindow = null; // allow MainWindow to be garbage collected
ShowDialog();
}
else
{
Application.Current.Shutdown();
}
Edit: Some background, I am loading large amounts of binary data from database, so when the user closes that window I need to reclaim that memory. At times opening a Window is allocating 500MB or more.
Adding the following section in the closing event handler seems to have helped, but still doesn't explain why the window itself remains in memory.
private void MetroWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// destroy render window
_miniMainModelSession.DeleteRenderWindow();
// dispose of native resources
_miniRenderer.Dispose();
_miniRenderer = null;
PreviewRenderer.Dispose();
// deallocate memory
_dialog = null;
_patientShapes = null;
_miniMainModelSession = null;
DialogResult = _cancelClose;
OmegaApp.Instance.UnregisterControl("PatientFolder");
// collect any garbage explicity
GC.Collect();
}