简体   繁体   English

Contentcontrol.Content内存泄漏中的WPF用户控件

[英]WPF Usercontrol in Contentcontrol.Content Memory Leak

The problem is a memory leak in my prism, autofac .NET 4.5.1 WPF application. 问题是我的棱镜autofac .NET 4.5.1 WPF应用程序中发生内存泄漏。 I reduced the problem and the problem is also in a native WPF application without prism and autofac. 我减少了这个问题,这个问题也出现在没有棱镜和autofac的本地WPF应用程序中。

On startup the application uses 34 MB ram. 启动时,应用程序使用34 MB内存。 I set a new instance of a usercontrol with a big ram allocation in the contentcontrol in the mainwindow. 我在主窗口的contentcontrol中设置了一个具有大量内存分配的usercontrol新实例。 The ram goes up to 900 MB. 内存高达900 MB。 Now i will clean up my ui and i tested to clear the internal collection in the usercontrol, the content of the contentcontrol set to null ... But the ram is on 140 MB. 现在,我将清理用户界面,并进行测试以清除usercontrol中的内部集合,将contentcontrol的内容设置为null ...但是ram的大小为140 MB。 How can i release the ram between the startup and after cleanup. 我如何在启动和清理后释放RAM。

When i create the usercontrol and not set it in the content of the contentcontrol the ram goes to 38 MB. 当我创建usercontrol而不在contentcontrol的内容中设置它时,内存达到38 MB。 That is OK. 那没问题。 It is possible to release the "complete" ram when i use a usercontrol in another control? 当我在另一个控件中使用usercontrol时,可以释放“ complete” ram吗?

Mainwindow: 主窗口:

<ContentControl x:Name="myContentControl"></ContentControl>

Code behind: 后面的代码:

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    this.myContentControl.Content = new UserControl1();
}

private void Button_Click_2(object sender, RoutedEventArgs e)
{
    this.myContentControl.Content = null;
    GC.Collect();
}

The usercontrol: 用户控件:

<Grid>
    <DataGrid x:Name="myTestListView" Background="Purple" Height="280></DataGrid>
</Grid>

Code: 码:

public partial class UserControl1 : UserControl
{
    private List<string> Testitems = new List<string>(); 

    public UserControl1()
    {
        InitializeComponent();
        this.Testitems = new List<string>();
        for (int i = 0; i < 1000000; i++) this.Testitems.Add(i.ToString());
        this.myTestListView.ItemsSource = this.Testitems;
    }
}

考虑用ObservableCollection替换Usercontrol1集合后备字段,该泄漏将消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM