【发布时间】:2013-08-05 06:23:39
【问题描述】:
我正在创建简单的 WPF 测试项目,其中包含多个 UserControls(Insteda of Pages)。我正在使用 Switcher 类在不同的 UserControls 之间导航。当我导航到不同的页面时,我观察到每个 UserControle 的内存消耗都在增加不调用 Navigation 和 GC。
1.So am i doing something wrong in following code?
2.Which part of the code consuming more memory?
3.Do i need to invoke GC for disposing my UserControls on each new UserControle creation?
If need how can i invoke GC?
public void On_Navigate_Click()
{
UserControle newusercontrole=new UserControle();
DataSet ds = new DataSet();
ds=con.getSome_Datafrom_SQL();//Gets data from SQL via connection class
dataGrid_test.ItemsSource = ds.Tables[0].DefaultView;
Grid.SetColumn(newusercontrole, 1);//dataGrid_test is inside newusercontrole and following is the code to add "this" usercontrol to the main window.
Grid.SetRow(newusercontrole, 1);
Grid.SetZIndex(newusercontrole, 10);
Container.Children.Add(newusercontrole);
}
【问题讨论】:
-
您正在创建一个新的用户控件,并在每次单击按钮时将其放入子集合中。以前的 UC 是如何从 children 集合中删除的?如果还有对prev的引用。 UC、GC 不会收集。
-
是的,现在我更改了我的代码。在添加新的 UserControl 之前,我正在删除以前的 UC,如下所示。 Container.Children.Remove(oldusercontrole);他们仍然有很多内存泄漏。
-
Container.Children.Remove(oldusercontrole);它的工作..谢谢..
标签: wpf user-controls garbage-collection