简体   繁体   English

WPF DataGrid ItemsSource memory 泄漏

[英]WPF DataGrid ItemsSource memory leak

I don't know why but when I define the ItemsSource of my DataGrid, it creates some memory leak.我不知道为什么,但是当我定义我的 DataGrid 的 ItemsSource 时,它会创建一些 memory 泄漏。

This is the xaml code of my DataGrid:这是我的 DataGrid 的 xaml 代码:

<DataGrid x:Name="DataGrid1" AutoGenerateColumns="True" IsReadOnly="True" ClipboardCopyMode="ExcludeHeader" Margin="20,250,20,20" SelectionUnit="Cell" Style="{DynamicResource MaterialDesignDataGrid}" SelectionMode="Single"/>

And this is the code behind where I define the ItemsSource:这是我定义 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

As you can see from the diagnostic tool, I have some memory leak somewhere.正如您从诊断工具中看到的那样,我在某处有一些 memory 泄漏。 在此处输入图像描述

I know it's caused by the line DataGrid1.ItemsSource = dt.DefaultView because if I comment it, the memory leak doesn't occur.我知道这是由DataGrid1.ItemsSource = dt.DefaultView行引起的,因为如果我对其进行评论,则不会发生 memory 泄漏。

I've downloaded JetBrains dotMemory to see what's going on.我已经下载了 JetBrains dotMemory 看看发生了什么。 As you can see bellow, it tells me that it's caused by DataGridRow.正如您在下面看到的,它告诉我它是由 DataGridRow 引起的。 在此处输入图像描述

I don't understand why this happens.我不明白为什么会这样。 The data is correct in the DataTable.数据表中的数据是正确的。 在此处输入图像描述

If you have any clues, please share them with me如果您有任何线索,请与我分享

Found.成立。 Actually it came from the fact that my DataGrid was in a StackPanel.实际上它来自我的 DataGrid 在 StackPanel 中的事实。 And I still don't know why but it was a problem.我仍然不知道为什么,但这是一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM