I have done and very easy test, just to understand the wpf how works with memory.
I create one project with one window where's a Button
.
And a second window totally empty.
when I press the Button
in click open the second window
code behind window 1:
/// <summary>
/// Interaction logic for WindowTest1.xaml
/// </summary>
public partial class WindowTest1 : Window
{
public WindowTest1()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var wt2 = new WindowTest2();
wt2.ShowDialog();
wt2 = null;
}
}
xaml window 1:
<Window x:Class="WpfAppXtesting.WindowTest1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfAppXtesting"
mc:Ignorable="d"
Title="WindowTest1" Height="450" Width="800">
<Grid>
<Button Content="Button" HorizontalAlignment="Left" Height="148" Margin="191,138,0,0" VerticalAlignment="Top" Width="267" Click="Button_Click"/>
</Grid>
code behind window2:
/// <summary>
/// Interaction logic for WindowTest2.xaml
/// </summary>
public partial class WindowTest2 : Window
{
public WindowTest2()
{
InitializeComponent();
}
}
xaml code window2:
<Window x:Class="WpfAppXtesting.WindowTest2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfAppXtesting"
mc:Ignorable="d"
Title="WindowTest2" Height="450" Width="800">
<Grid>
</Grid>
In the image below I took a screenshot of memory status. the first line I took when start only the first window. the second line when the second window was open. the third line when the second window was close. the last list I took after ten time open and close the second window.
Why the memory don't come back to the first list usage?