WPF 不释放内存
我的应用程序中存在一个问题,即当用户控制时内存未释放 已卸载。这会将内存从 40MB 开始增加到 200MB 甚至更多。
为了模拟这个,
- 我创建了一个wpf项目,它有主窗口和一个用户控件,
- 将1000个对象加载到wpf数据网格中,该数据网格放置在用户控件中
- 滚动查看器放置在主窗口中
- 用户控件加载在此滚动查看器内单击“显示”按钮后,
- 单击“关闭”按钮后,用户控件将从滚动查看器的内容中删除。
我与任务管理器检查后,在将 1000 个对象加载到网格之前,内存消耗为 14MB。单击显示按钮加载后,它会增加到 70MB。但是当我单击“关闭”按钮从窗口中删除用户控件时,内存仅减少到 67MB。难道不应该减少到 14BMB 或接近的值吗?
当我使用 ANTS 内存分析器检查这一点时,它显示即使从窗口中删除用户控件后,1000 个对象仍保留在内存中。当用户控件从窗口中删除时(一旦滚动查看器内容属性设置为空),垃圾收集器不应该释放这些对象吗?
以下是我用于此目的的代码。我没有使用任何样式、数据模板或任何第三方控件,仅使用 WPF DataGrid 控件来加载数据。
背后的用户控制代码
public partial class UserControl1 : UserControl,IDisposable
{
List<TestClass> list = null;
public UserControl1()
{
InitializeComponent();
}
public void Dispose()
{
BindingOperations.ClearBinding(dgList, DataGrid.ItemsSourceProperty);
list.Clear();
GC.Collect();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
list = new List<TestClass>();
for (int i = 0; i < 1000; i++)
{
TestClass obj = new TestClass();
obj.Name = "test name";
obj.Age = 34;
list.Add(obj);
}
dgList.ItemsSource = list;
}
}
public class TestClass
{
public string Name { get; set; }
public int Age { get; set; }
}
背后的主窗口代码
public partial class MainWindow : Window
{
UserControl1 control = null;
public MainWindow()
{
InitializeComponent();
}
private void btnClose_Click(object sender, RoutedEventArgs e)
{
control.Dispose();
scwContent.Content = null;
control = null;
}
private void btnShow_Click(object sender, RoutedEventArgs e)
{
control = new UserControl1();
scwContent.Content = control;
}
}
谢谢。
I have a issue in our application where the memory is not released when the user controls
are unloaded. This will increase the memory starting from 40MB and ends up with 200MB and more.
To simulate this,
- I created a wpf project which has the main window and a user control
- loaded 1000 objects into a wpf datagrid which is placed in a user control
- A scroll viewer is put in the main window
- The user control is loaded inside this scroll viewer Once the show button is clicked
- The user control is removed from the Content of the Scroll viwer once the Close button is clicked
Once i checked with the task manager, before the 1000 objects are loaded to the grid, the memory consumption is 14MB. Once its loaded by clicking on the show button it increases to 70MB. But when i click on Close button to remove the user control from the window, the memory reduces to 67MB only. Shouldn't it reduce to 14BMB or something close to that??
When i checked this with the ANTS memory profiler, it shows that the 1000 objects remain in memory even after the User control is removed from the Window. Shouldn't the garbage collector release these objects when the user control is removed from the window (once the Scroll viewer Content Property is set to null)?
Following is the Code i used for this. I didn't use any styles, data templates or any third party controls, only used the WPF DataGrid control to load the data.
The User Control Code Behind
public partial class UserControl1 : UserControl,IDisposable
{
List<TestClass> list = null;
public UserControl1()
{
InitializeComponent();
}
public void Dispose()
{
BindingOperations.ClearBinding(dgList, DataGrid.ItemsSourceProperty);
list.Clear();
GC.Collect();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
list = new List<TestClass>();
for (int i = 0; i < 1000; i++)
{
TestClass obj = new TestClass();
obj.Name = "test name";
obj.Age = 34;
list.Add(obj);
}
dgList.ItemsSource = list;
}
}
public class TestClass
{
public string Name { get; set; }
public int Age { get; set; }
}
Main Window Code behind
public partial class MainWindow : Window
{
UserControl1 control = null;
public MainWindow()
{
InitializeComponent();
}
private void btnClose_Click(object sender, RoutedEventArgs e)
{
control.Dispose();
scwContent.Content = null;
control = null;
}
private void btnShow_Click(object sender, RoutedEventArgs e)
{
control = new UserControl1();
scwContent.Content = control;
}
}
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
垃圾收集器仅在需要内存时收集,而不是在引用设置为 null 时收集。
(唯一的例外:调用
GC.Collect()
)The Garbage collector only collects when memory is needed, not when references are set to null.
(Only exception: calling
GC.Collect()
)你为什么要打电话给GC?这不是必需的
更改
为
您可以将列表对象设置为 null ..
阅读本文以了解将对象设置为 null
将对象设置为 null 与 Dispose()
Why are you trying to call GC? That is not required
Change
To
You can set the list object to null ..
Read this for understanding setting objects to null
Setting an object to null vs Dispose()