【问题标题】:WPF DataGrid ItemsSource memory leakWPF DataGrid ItemsSource 内存泄漏
【发布时间】: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


【解决方案1】:

找到了!实际上它来自我的 DataGrid 在 StackPanel 中的事实。我仍然不知道为什么,但这是个问题。

【讨论】:

    猜你喜欢
    • 2017-12-02
    • 1970-01-01
    • 2014-09-01
    • 2011-02-21
    • 1970-01-01
    • 2010-12-05
    • 2011-03-19
    相关资源
    最近更新 更多