I have a very bad performance issue. I´m currently working on a multi-window application, but when I close one of the windows no memory is deallocated.
There are even controls I instantiate in code and after I used them I assigned null to their fields but nothing. The GC refuses to collect the control although there is no reference on it any more.
Googling that problem brought me to Application.Exit
and Dispatcher.ShutdownStarted
but they do not solve my problem, cause the application keeps on running.
I made a small sample to show you my problem:
MainPage.xaml
<Window x:Class="FinalizerSampleProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:FinalizerSampleProject"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Button Content="Get rid of the custom control!" Grid.Row="1" Click="Button_Click" x:Name="DummyButton" />
</Grid>
</Window>
MainPage.xaml.cs
using System;
using System.Windows;
namespace FinalizerSampleProject
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private CustomControl control = new CustomControl();
public MainWindow()
{
InitializeComponent();
LayoutRoot.Children.Add(control);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (control != null)
{
LayoutRoot.Children.Remove(control);
control = null;
DummyButton.Content = "Show how many instances of CustomControl still exist.";
GC.Collect();
}
else
{
MessageBox.Show(CustomControl.InstanceCounter.ToString());
}
}
}
}
CustomControl.xaml
<UserControl x:Class="FinalizerSampleProject.CustomControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Background="LightGreen">
<TextBlock Text="Hi, I'm a custom control!"/>
</UserControl>
CustomControl.xaml.cs
using System.Windows.Controls;
namespace FinalizerSampleProject
{
/// <summary>
/// Interaction logic for CustomControl.xaml
/// </summary>
public partial class CustomControl : UserControl
{
public static int InstanceCounter = 0;
public CustomControl()
{
InstanceCounter++;
InitializeComponent();
}
~CustomControl()
{
InstanceCounter--;
}
}
}
If you try it, you will see that this counter every time appears to be "one". I have no idea how to get rid of that object instance.