3

我红了很多关于数据网格泄漏的文章,但找不到正确的方法。这是一些要重现的代码。它有数据网格和按钮,每次按下按钮,它都会用随机数填充表格,并占用 10mb 的内存,并且不会释放它。绑定到 TextBlock 不会导致内存泄漏,但我需要文本框双向绑定。

<Window x:Class="test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <DataGrid Name="dg" Grid.Row="0" AutoGenerateColumns="False" ItemsSource="{Binding}">
        <DataGrid.Columns>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Text0}"></TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Text1}"></TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Text2}"></TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Text3}"></TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Text4}"></TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Text5}"></TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Text6}"></TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Text7}"></TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Text8}"></TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Text9}"></TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
    <Button Content="update" Grid.Row="1" Width="Auto" Height="Auto" Click="Button_Click"></Button>
</Grid>

namespace test
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();


    }

    public ObservableCollection<obj> res;
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        res = new ObservableCollection<obj>();


        for (var i = 0; i < 20; i++)
        {
            res.Add(new obj
            {
                Text0 = ((new Random()).NextDouble() * 100).ToString(),
                Text1 = ((new Random()).NextDouble() * 100).ToString(),
                Text2 = ((new Random()).NextDouble() * 100).ToString(),
                Text3 = ((new Random()).NextDouble() * 100).ToString(),
                Text4 = ((new Random()).NextDouble() * 100).ToString(),
                Text5 = ((new Random()).NextDouble() * 100).ToString(),
                Text6 = ((new Random()).NextDouble() * 100).ToString(),
                Text7 = ((new Random()).NextDouble() * 100).ToString(),
                Text8 = ((new Random()).NextDouble() * 100).ToString(),
                Text9 = ((new Random()).NextDouble() * 100).ToString(),
            });
        }
        dg.DataContext = res;
    }


}
public class obj : INotifyPropertyChanged
{
    private string text0;
    public string Text0
    {
        get
        {
            return text0;
        }
        set
        {
            text0 = value;
            NotifyPropertyChanged("Text0");
        }
    }
    private string text1;
    public string Text1
    {
        get
        {
            return text1;
        }
        set
        {
            text1 = value;
            NotifyPropertyChanged("Text1");
        }
    }
    private string text2;
    public string Text2
    {
        get
        {
            return text2;
        }
        set
        {
            text2 = value;
            NotifyPropertyChanged("Text2");
        }
    }
    private string text3;
    public string Text3
    {
        get
        {
            return text3;
        }
        set
        {
            text3 = value;
            NotifyPropertyChanged("Text3");
        }
    }
    private string text4;
    public string Text4
    {
        get
        {
            return text4;
        }
        set
        {
            text4 = value;
            NotifyPropertyChanged("Text4");
        }
    }
    private string text5;
    public string Text5
    {
        get
        {
            return text5;
        }
        set
        {
            text5 = value;
            NotifyPropertyChanged("Text5");
        }
    }
    private string text6;
    public string Text6
    {
        get
        {
            return text6;
        }
        set
        {
            text6 = value;
            NotifyPropertyChanged("Text6");
        }
    }
    private string text7;
    public string Text7
    {
        get
        {
            return text7;
        }
        set
        {
            text7 = value;
            NotifyPropertyChanged("Text7");
        }
    }
    private string text8;
    public string Text8
    {
        get
        {
            return text8;
        }
        set
        {
            text8 = value;
            NotifyPropertyChanged("Text8");
        }
    }
    private string text9;
    public string Text9
    {
        get
        {
            return text9;
        }
        set
        {
            text9 = value;
            NotifyPropertyChanged("Text9");
        }
    }

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
}

}

4

2 回答 2

1

TextBox在 WPF 中有一个与之关联的Undo\Redo堆栈,它存储可重做/可撤消的值,因为它们更改为新值。这可能解释了TextBoxes 的释放内存。

现在您可能会问,您是否ItemsSource每次都设置新鲜。那不是应该重新生成新的TextBoxes(因此新生成TextBox的没有机会持有任何 Undo堆栈)。我猜想Datagrid回收ItemsContainer和重用生成的TextBoxes.

但这只是我的猜测!!!

您可以尝试限制Undo堆栈并重新检查它是否适合您...

http://social.msdn.microsoft.com/Forums/en/wpf/thread/376f7765-1c70-4289-a59b-e27c168a0fa2

于 2012-10-26T05:41:42.533 回答
1

如果您可以使用 DataGridTextColumn 而不是 DataGridTemplateColumn 您的内存将不会泄漏。

不知道为什么 DataGridTemplateColumn 有内存泄漏

编辑:

我也会只初始化一次 OberservableColletion,只设置一次数据上下文并在 button_click 中使用 clear。

编辑:DataGridTemplateColumn 的内存泄漏问题似乎在 .NET4.5 中消失了

于 2012-10-26T06:53:03.493 回答