Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

Is this a .NET bug?

I've got a form which contains a ComboBox. Then I'm creating an ArrayList which contains a set of objects of a class (let's say Country). Then set the DataSource of the ComboBox to be the array list like so:

myComboBox.DataSource = countryArrayList;

this works fine, and as expected the text appearing for each item is the ToString() method of the Country class...

However, when I close the form containing the combobox, the memory taken up by the countryArrayList is never freed -- I assume the references to the Country objects aren't being destroyed so therefore the garbage collector doesn't clean up.

To get around it I've had to set the DataSource of the combobox to null and then Dispose it manually on the Form.Closed event.

Has anyone else seen this problem before? Could it be a bug? -- I assume that when a form is closed, all controls should be disposed automatically and all datasources attached to them freed up?


Marcus Greenwood
Wednesday, October 15, 2003

You're micro-optimising I think. The garbage collector will free memory when it decides it's necessary, not when you think it should necessarily. If you want it to force a garbage collection (and 9 times out of 10 you really shouldn't bother): call GC.Collect().

Duncan Smart
Saturday, October 18, 2003

How are you able to tell when the memory used by the Country objects is freed?

It is my understanding that the Dispose pattern does not actually reclaim the memory used by an object.  This is still the job of the garbage collector.  Instead, using a Dispose() method is a way of explicitly allowing for cleanup of an unmanaged resource, such as an OS handle.

Setting an object reference to null does not free up the memory used either.  It's only purpose is to remove the object reference from the list of application roots.  The garbage collector uses this list of "active objects" when a garbage collection occurs to decide which objects are still in use.  If a previously allocated object (or other reference type) is no longer referenced, then it is made available for collection.

Guy Incognito
Sunday, October 19, 2003

Seems like the best way to tell would be to test this procedure with an endless loop and see if you crash running out of memory, or if it begins to get garbage-collected at some point.

Tuesday, October 21, 2003

*  Recent Topics

*  Fog Creek Home