【发布时间】:2022-01-17 20:19:04
【问题描述】:
我不知道为什么,但是当我定义 DataGrid 的 ItemsSource 时,它会造成一些内存泄漏。
这是我的 DataGrid 的 xaml 代码:
<DataGrid x:Name="DataGrid1" AutoGenerateColumns="True" IsReadOnly="True" ClipboardCopyMode="ExcludeHeader" Margin="20,250,20,20" SelectionUnit="Cell" Style="{DynamicResource MaterialDesignDataGrid}" SelectionMode="Single"/>
这是我定义 ItemsSource 的代码:
cmd = New SqlCommand With {
.CommandText = strsql,
.Connection = DBConn.ADONETconn
}
da = New SqlDataAdapter(cmd)
dt = New DataTable("RECH")
da.Fill(dt)
DataGrid1.ItemsSource = dt.DefaultView
我知道这是由DataGrid1.ItemsSource = dt.DefaultView 行引起的,因为如果我评论它,就不会发生内存泄漏。
我已经下载了 JetBrains dotMemory 来看看发生了什么。 正如您在下面看到的,它告诉我它是由 DataGridRow 引起的。
如果你有任何线索,请与我分享
【问题讨论】:
-
您能追踪保留路径吗? JetBrains Profiler 可以做到这一点。
-
调用树帮助我找到了问题。谢谢。
-
看起来
DataView有一个 Dispose 方法。
标签: c# wpf vb.net memory-leaks datagrid