This is a C# WinForm question. I have a MDI child form, when the form is opened, it will generate many tables to display in a DataGridView. The tables can be big and I use DataBinding to connect the tables to the DataGridView. When I close the form, I notice that the memory taken by the form is reclaimed timely.
I use the normal way of showing the MDI child form as:
var f = new FormBigMemory(objPassedIn);
f.Show();
As shown here, I cannot explicitly call the Dispose() method of the form. I assume that when f is out of its life circle, .net will automatically reclaim the memory it takes.
Since the form takes a lot memory, I want to explicitly call GC.Collect(); (I know it may not be a good idea to do so). In order to do this, I change the code to show the form in dialog model using the following code:
using(var f = new FormBigMemoryDialog(objPassedIn);
{
f.ShowDialog();
}
GC.Collect();
I am very disappointed to see that the memory taken by the form isn't reclaimed after GC.Collect() is called. I use memory profiler tool to get the snapshot of the memory after the form is closed and GC.Collect() is called. The biggest memory is held up by a BindingList! I really don't understand: if the whole form is disposed, why the BindingList still exists in the memory. I have checked my code and there is no where the BindingList reference is leaked out of the form.
Any idea why this wired thing happens to me? Many thanks to any suggestion and tips on .net memory management.