WPF每次打开和关闭窗口时都会增加内存 [英] WPF increase memory each time open and close window

查看:814
本文介绍了WPF每次打开和关闭窗口时都会增加内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了一个非常简单的测试,只是为了了解wpf如何与内存一起工作. 我创建一个有一个Button窗口的项目. 第二个窗口完全是空的. 当我按下Button时,单击打开第二个窗口 窗口1后面的代码:

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窗口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后面的代码:

code behind window2:

 /// <summary>
/// Interaction logic for WindowTest2.xaml
/// </summary>
public partial class WindowTest2 : Window
{
    public WindowTest2()
    {
        InitializeComponent();
    }
}

xaml代码window2:

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?

推荐答案

首先,在关闭第二个窗口后,您不调用GC.Collect强制立即进行垃圾收集,因此您不能假定到您上一次拍摄内存快照时,窗口已被收集.

First of all you don't call GC.Collect to force an immediate garbage collection after you have closed the second window, so you can't assume that the window has been collected by the time you take the last memory snapshot.

即使您确实显式调用了GC.Collect,也不能认为CLR实际上会立即释放占用的内存.就性能而言,分配和释放内存并不是免费的,并且由于应用程序以后会请求更多内存而发生了变化,因此没有必要立即将内存段释放回操作系统(OS).

And even if you did call GC.Collect explicitly, you can't assume that the CLR will actually release the occupied memory immediately. Allocating and freeing memory isn't free in terms of performance and as there is a change that the application will request more memory later on, there is no point of releasing the memory segment back to the operating system (OS) immediately.

.NET应用程序在托管环境中执行,CLR负责分配内存并将其释放回OS.这是如何以及何时执行的,是您无法在应用程序中真正控制的实现细节.

.NET applications are executed in a managed environment and the CLR is in charge of allocating memory and releasing it back to the OS. How and when it does this is an implementation detail that you can't really control in your application.

这里唯一需要注意的是,关闭的窗口一旦关闭就应该可以进行垃圾收集.是的.返回ShowDialog()方法后,将局部变量设置为null没什么区别.

The only thing to care about here is that the closed window should be eligible for garbage collection once it has been closed. And it is. Setting the local variable to null once the ShowDialog() method has returned makes no difference.

这篇关于WPF每次打开和关闭窗口时都会增加内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆