Skip to content

Conversation

@LeafShi1
Copy link
Member

@LeafShi1 LeafShi1 commented Jan 20, 2026

Fixes #14204

Root Cause

The foreach loop over DataGridViewRowCollection uses an enumerator that calls this[index], which unshares (clones) each shared row. For 700k rows, this causes massive memory allocation and CPU usage.

Proposed changes

  • DataGridView.Methods.cs:
    • Added ReleaseRowUiaProviders() helper method
    • Modified ReleaseUiaProvider() to use HashSet deduplication instead of foreach
  • DataGridViewColumnCollection.cs:
    • Modified RemoveAtInternal() to use HashSet deduplication

Customer Impact

  • Applications with large DataGridView (700k+ rows) freeze for several seconds when closing forms or removing columns

Regression?

  • Yes (The regression was introduced in this commit)

Risk

  • Minimal

Screenshots

Before

Closing a form with 700k rows causes 5-10+ seconds of UI freeze and high memory spike due to row cloning.

After

Form closes near-instantly with minimal memory overhead.

Test methodology

  • Manual test

Test environment(s)

  • .net 11.0.0-alpha.1.26057.107
Microsoft Reviewers: Open in CodeFlow

@LeafShi1 LeafShi1 requested a review from a team as a code owner January 20, 2026 10:21
@LeafShi1 LeafShi1 marked this pull request as draft January 20, 2026 10:21
@LeafShi1
Copy link
Member Author

LeafShi1 commented Jan 20, 2026

This fix has been tested and verified.

@codecov
Copy link

codecov bot commented Jan 21, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.40479%. Comparing base (15506ad) to head (2394af7).
⚠️ Report is 26 commits behind head on main.

Additional details and impacted files
@@                 Coverage Diff                  @@
##                main      #14224          +/-   ##
====================================================
+ Coverage   77.15242%   97.40479%   +20.25237%     
====================================================
  Files           3279        1214        -2065     
  Lines         645333      357428      -287905     
  Branches       47720        5598       -42122     
====================================================
- Hits          497890      348152      -149738     
+ Misses        143757        8484      -135273     
+ Partials        3686         792        -2894     
Flag Coverage Δ
Debug 97.40479% <ø> (+20.25236%) ⬆️
integration ?
production ?
test 97.40479% <ø> (-0.00080%) ⬇️
unit ?

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@LeafShi1 LeafShi1 marked this pull request as ready for review January 22, 2026 01:48
@dotnet-policy-service dotnet-policy-service bot removed the draft draft PR label Jan 22, 2026
@LeafShi1 LeafShi1 requested a review from Copilot January 22, 2026 08:06
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a critical performance issue in DataGridView where applications with large datasets (700k+ rows) experience severe UI freezes when closing forms or removing columns. The root cause was that iterating over the DataGridViewRowCollection using foreach would unshare (clone) each shared row, causing massive memory allocation and CPU usage.

Changes:

  • Replaced inefficient foreach loops that trigger row cloning with HashSet-based deduplication using direct SharedList access
  • Added a helper method ReleaseRowUiaProviders() to centralize row UIA provider cleanup logic
  • Applied the same optimization pattern to both form disposal and column removal scenarios

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
DataGridView.Methods.cs Added ReleaseRowUiaProviders() helper and modified ReleaseUiaProvider() to use HashSet deduplication when iterating over rows, preventing costly row cloning
DataGridViewColumnCollection.cs Modified RemoveAtInternal() to use HashSet deduplication when releasing UIA providers for cells in the removed column

SimonZhao888
SimonZhao888 previously approved these changes Jan 23, 2026
@SimonZhao888 SimonZhao888 dismissed their stale review January 23, 2026 09:15

Please fix the failure test cases firstly.

@LeafShi1 LeafShi1 force-pushed the Issue_14204_fix_DataGridView_Excessive_CPU+memory_usage branch from 25e89dc to 2394af7 Compare January 26, 2026 07:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DataGridView: Excessive CPU+memory usage when closing form

2 participants