【发布时间】:2018-11-26 10:30:09
【问题描述】:
我已经完成了非常简单的测试,只是为了了解 wpf 如何使用内存。
我用一个窗口创建了一个项目,其中有一个Button。
第二个窗口完全是空的。
当我按下Button 点击打开第二个窗口
窗口 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 窗口 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>
window2 后面的代码:
/// <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>
在下图中,我截取了内存状态的屏幕截图。 我只启动第一个窗口时采取的第一行。 第二个窗口打开时的第二行。 第二个窗口关闭时的第三行。 我在打开和关闭第二个窗口十次后得到的最后一个列表。
为什么内存没有回到第一个列表使用量?
【问题讨论】:
-
是的,您已经创建了一个测试来制造垃圾,垃圾收集器会在需要时清理它。你应该参观一下这个网站docs.microsoft.com/en-us/dotnet/standard/garbage-collection/…
-
Why the memory don't come back to the first list usage?你是怎么想到它会回到第一个列表使用的? -
因为第二个窗口在关闭时应该释放其占用的资源。
标签: c# wpf xaml memory heap-memory