Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Data gridview rows with combobox cells are not being freed when rows are cleared and setting column data source to null takes a long time

$
0
0

Howdy Winforms dudes,

        I have an application where we make extensive use of Datagridviews that we manually clear and fill.

I noticed when it came to clearing and filling the dgv that we had a memory leak, and after using the built in profilers as well as weak references I found that the DGV rows are not getting garbage collected when we clear the grid.

After a bit of research I found that it had to do with combox columns, and their datasource; so when we cleared the grid I'd go and set all the combobox column datasources to nothing. This allowed the grid to clear but on a grid with 40k rows this took 5 minutes in the main thread which is unacceptable.

I tried moving it out of the main thread and into a background worker by making this change so the main thread can go on and the user can keep using

  1. Copy all rows to a new array
  2. clear the dgv so it can be refilled
  3. pass the array to a background worker or thread pool
  4. The thread iterates over the rows and then sets the datasource of every combobox cell to nothing

This works, and eventually the memory is freed, but in a thread this takes 15 minutes! And accoridng to the profiler almost all the time is spent on

comboboxcell.Datasource = nothing

and even worse for some reason clearing the datasource whether done column by column or cell by cell causes memory usage to temporarily go up; which means some event or something must be happening.

So is there a way to

1. Clear rows with comboboxes from a dgv in a way they will get gced?

or

2. Make it so comboboxcell.Datasource = nothing doesn't take a long time? I mean the row is already removed from the DGV so is there some other event to remove?


-Andrew


Viewing all articles
Browse latest Browse all 2535

Trending Articles