I am having a very strange memory leak that seems related to databinding. It is very hard to reproduce, so I won't post any code here to do so, but will just describe the problem.
We have data entry forms which have controls which bind to custom business objects through a BindingSource object. On Dispose of the form, we call ClearBindings as described in:
http://msdn.microsoft.com/library/default.asp url=/library/en-us/dnwinforms/html/databinding_winforms10_11.asp#databinding_winforms10_11_topic11
However, our business object stay in memory after the form has been closed. Using a memory profiling tool, I can see that there is a root path to our business object:
[Business Object]
System.Collections Hashtable.bucket[]
System.Collections Hashtable
System.ComponentModel ReflectPropertyDescriptor
System.ComponentModel PropertyDescriptor[]
System.Collections Hashtable.bucket[]
System.Collections Hashtable
System.ComponentModel ReflectTypeDescriptionProvider _propertyCache
I have tried calling TypeDescriptor.Refresh every way I know, but the cache of our object is not clearing out. BTW this is with .NET 2.0.
Any and all help will be appreciated!

Managed memory leak
DCWCore
Don't know if you are still working on this, or got a fix - but:
Assembly systemAssembly = typeof(System.ComponentModel.Component).Assembly; Type reflectTypeDescriptionProviderType = systemAssembly.GetType("System.ComponentModel.ReflectTypeDescriptionProvider");_propertyCacheInfo = reflectTypeDescriptionProviderType.GetField(
"_propertyCache", BindingFlags.Static | BindingFlags.NonPublic); Hashtable _propertyCache = (Hashtable)_propertyCacheInfo.GetValue(null);_propertyCache.Clear();
will sort you out.
I have decreased our memory usage by 75% by 'tuning' caches that do not free objects, however haven't had actual instances of my own classes stuck (only RunTimeMethodInfo instances and the like)
SSRS Jon
I AM HAVING THE EXACT!!! SAME PROBLEM!
but mine is with my MAIN FORM (the parent form that is open all the time) and a Grid on the form that is getting Re-Bound to my business collection object every so often. I can recreate it and everything... here is my code
I set the BindingSource to Nothing... then if I have a valid BO... set it to the BindingSource (uibt_bsLineItems.DataSouce)... this in turn updates the grid. After about thirty to fifty times of this method getting fired... it hangs up!
----------------------
uibt_bsLineItems.DataSource =
Nothing If customer IsNot Nothing AndAlso customer.HasActiveInvoiceAndLineItems ThenDebug.Print("Before LineItems Datasource SET 1")
uibt_bsLineItems.DataSource = customer.ActiveInvoice.LineItems
End Ifuits_grdInvoiceProductItems.DataBind()
---------------------------
Leonard Lee
That path shows that the TypeDescriptor system is keeping a reference to my object in its _propertyCache. The way to clean out this cache, as I noted in the MSDN article I referenced, is to call TypeDescriptor.Refresh(component), where 'component' is the bound object (my business object). This did not work. I am trying to find out why the TypeDescriptor is keeping this reference to my object is memory.
Surya Suluh
Have you contacted Microsoft I have pretty much given up on this problem for now until I have some time to call a rep and get it sorted out...
Mike36
Just as a point here - the object may remain in memory long after you have disposed of the form. This is because .NET uses a process called the garbage collection process which will periodically come along and find all unreferable objects in memory and reclaim the memory.
So if you objects are no longer referencable in you code then eventually they will be recovered.
When does this garbage collection process happen This is a interesting point in that it happens when the system is short on resources and needs to try and find some more - so it will run and clean up memory, It would also occur periodically although not really at pre-determined time periods.
So why is this good, because under COM which used reference counting you could get memory leaks if something crashed and the memory not longer had a reference or you had some form of cyclic reference which would mean the memory would be formever held. The garbage collection process does not use reference counting and is searching for objects that are no longer referencable and will reclaim the memory.
So you memory leak is probably just that its not yet been collected. I would hazard the guess thats why its difficult to reproduce. If you've got loads of resources (memory) then the garbage collection process will probably run a lot less frequently as it doesnt run short as often. If you have lots of processes running - resources become more important and the process will run more frequently.
There is loads of reference material, just do a web search on garbage collections + .NET