dotnet / dotnet/winforms

DataGridView: Excessive CPU+memory usage when closing form

Open
#14,204 6 comments 1 reaction 2 assignees Claimed by @KlausLoeffelmann View on GitHub
area-controls-DataGridView tenet-accessibility tenet-accessibility-performance
Dominant language
C#
Stars
4.9k
Forks
1.1k
Avg merge
1d 13m
Merged PRs (30d)
85

Description

The root cause seems to be that [this enumeration](https://github.com/dotnet/winforms/blob/1e206698946b9ccc026c045f73ac5b792db4af92/src/System.Windows.Forms/System/Windows/Forms/Controls/DataGridView/DataGridView.Methods.cs#L25605) clones every row and cell in the DataGridView.

The result is it takes ~12 minutes and ~8 GB extra memory just to _dispose_ a DataGridView with ~700k rows.

Abbreviated stack trace:
[DataGridViewRowCollection.this[int].get(int index) Line 137](https://github.com/dotnet/winforms/blob/main/src/System.Windows.Forms/System/Windows/Forms/Controls/DataGridView/DataGridViewRowCollection.cs#L137)
[DataGridView.ReleaseUiaProvider(HWND handle) Line 25605](https://github.com/dotnet/winforms/blob/1e206698946b9ccc026c045f73ac5b792db4af92/src/System.Windows.Forms/System/Windows/Forms/Controls/DataGridView/DataGridView.Methods.cs#L25605)
Control.WmDestroy(ref Message m) Line 11463

**Workaround**: setting every DataGridView's DataSource = null in the FormClosing event eliminates the issue. That seems to imply there's another problem with consistency--if ReleaseUiaProvider needs to be called for every single cell during WmDestroy, why wouldn't it be needed when clearing the DataSource?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.