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
- Copy all rows to a new array
- clear the dgv so it can be refilled
- pass the array to a background worker or thread pool
- 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