我做了一个非常简单的测试,只是为了了解WPF如何与内存一起工作。
我创建了一个项目,其中有一个窗口,里面有一个按钮。
还有一个完全空的第二个窗口。
当我按下按钮时,点击打开第二个窗口。
窗口1后面的代码:
XAML 窗口 1:
XAML 代码窗口2:
为什么内存没有返回到第一个列表的使用情况?
/// <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 窗口 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>
窗口2的代码后端: /// <summary>
/// Interaction logic for WindowTest2.xaml
/// </summary>
public partial class WindowTest2 : Window
{
public WindowTest2()
{
InitializeComponent();
}
}
XAML 代码窗口2:
<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>
在下面的图片中,我截取了内存状态的屏幕截图。
第一行是仅打开第一个窗口时的截图。
第二行是第二个窗口打开时的截图。
第三行是关闭第二个窗口后的截图。
最后一列是在打开和关闭第二个窗口十次后拍摄的截图。为什么内存没有返回到第一个列表的使用情况?