question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WPF DataGrid memory leak in VS2022

See original GitHub issue

This issue has been moved from a ticket on Developer Community.


[severity:I’m unable to use this version] [regression] [worked-in:16.11.3] Original Source I have this code, and no matter what I do I keep getting a memory leak from the DataGrid data update. I’ve been looking at all the other answers to this problem here for days and I haven’t found anything that works for me. I have a WPF window and this code to update the data (it is a small version of what happens in the real code, but I get the same bug). Works fine in VS2019.

I have this WPF code:

<Window x:Class="TremendoMemoryLeak.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TremendoMemoryLeak"
        mc:Ignorable="d"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <DataGrid ItemsSource="{Binding Hmis, Mode=OneWay}" AutoGenerateColumns="False" IsReadOnly="True">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Estado">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Ellipse Fill="{Binding Estado}" HorizontalAlignment="Left" Height="25" Stroke="Black" VerticalAlignment="Top" Width="25"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="Dato1" Binding="{Binding Dato1}"/>
                <DataGridTextColumn Header="Dato2" Binding="{Binding Dato2}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

This class code:

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
    public ObservableCollection<Datos> Hmis { get; } = new ObservableCollection<Datos>();
    private System.Threading.Timer timer;

    public MainWindow()
    {
        InitializeComponent();
        timer = new System.Threading.Timer(ActualizacionUI_Tick, null, 1000, 1000);
    }

    public event PropertyChangedEventHandler? PropertyChanged;

    private void ActualizacionUI_Tick(object data)
    {
        Datos dato1 = new Datos() { Dato1 = "1", Dato2 = "2" };

        Datos dato2 = new Datos() { Dato1 = "1", Dato2 = "2" };

        Datos dato3 = new Datos() { Dato1 = "1", Dato2 = "2" };

        Dispatcher.Invoke(() =>
        {
            dato1.Estado = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF5DC75D"));
            dato2.Estado = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF5DC75D"));
            dato3.Estado = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF5DC75D"));
            Hmis.Clear();
            Hmis.Add(dato1);
            Hmis.Add(dato2);
            Hmis.Add(dato3);
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Hmis"));
        });
    }
}

And this data class:

public class Datos : INotifyPropertyChanged
{
    public Brush Estado { get; set; }
    public string Dato1 { get; set; }
    public string Dato2 { get; set; }

    public event PropertyChangedEventHandler? PropertyChanged;
}

Original Comments

Feedback Bot on 10/20/2021, 11:07 PM:

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.


Original Solutions

(no solutions)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sneik15commented, Oct 27, 2021

Probed in Framework versions 4.6.1/4.8 and net 5: Example SLN: TremendoMemoryLeak.zip Only happens in visual studio 2022

0reactions
sneik15commented, Nov 16, 2021

@singhashish-wpf I don’t know how to correctly implement a DependencyProperty version.

Read more comments on GitHub >

github_iconTop Results From Across the Web

WPF DataGrid memory leak · Issue #6087 · dotnet/wpf
The app adds a set of items to the ObservableCollection and then moves the same items around in a cycle. The result is...
Read more >
WPF DataGrid ItemsSource memory leak
I know it's caused by the line DataGrid1.ItemsSource = dt.DefaultView because if I comment it, the memory leak doesn't occur. I've downloaded ...
Read more >
Visual Studio 2022 Memory Leak - Microsoft Q&A
When I open 1 particular .Net 5 C# solution, VS 2022 memory rises up to 22GB and goes up and down. I have...
Read more >
[Solved] Rendering WPF Controls
I had serious problem with "virtualization memory leak" when I used regular data binding. Than I used DataTemplate and all performance ...
Read more >
Fighting Common WPF Memory Leaks with dotMemory
Fighting Common WPF Memory Leaks with dotMemory · Binding leak · Collection binding leak · Textbox undo leak · Event Handler leak ·...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found