在DataGridView中视内存泄漏 [英] Apparent Memory Leak in DataGridView
问题描述
你如何强迫一个DataGridView释放其引用到绑定的DataSet?
How do you force a DataGridView to release its reference to a bound DataSet?
我们必须显示在一个DataGridView一个相当大的数据集,并注意到,资源不是在DataGridView被关闭后,被释放。如果用户多次认为这一报告中,他们最终会得到一个内存不足的异常。蚂蚁内存探查证实是DGV尽管 dgv.DataSource
保持参照被设置为null。
We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the DataGridView was closed. If the user repeatedly views this report they eventually get an out of memory exception. ANTS Memory Profiler confirmed that the DGV is holding a reference despite dgv.DataSource
being set to null.
推荐答案
诀窍迫使DataGridView的释放资源是做通过中介对象的BindingSource
绑定。
The trick to forcing the DataGridView to release resources is to do the binding through the intermediary object BindingSource
.
然后,代码看起来是这样的:
The code then looks something like this:
...
DataGridView dgvQueryResults;
DataTable m_dataTable;
BindingSource m_binder;
public void PopulateView()
{
...
// Bind the data source through and intermediary BindingSource
m_binder.DataSource = m_dataTable;
dgvQueryResults.DataSource = m_binder;
...
}
/// <summary>
/// Frees lindering resources. Sets data bindings to null and forces
/// garbage collection.
/// </summary>
private void ResetDataGridView()
{
dgvQueryResults.DataSource = null;
if (null != m_binder) m_binder.DataSource = null;
m_binder = null;
dataTable = null;
// Force garbage collection since this thing is a resource hog!
GC.Collect ();
m_binder = new BindingSource ();
}
...
这篇关于在DataGridView中视内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!