I am using WPF.
After adding user controls dynamicaaly at runtime, How to Dispose that user control. Because there is no Dispose Method for User Controls. i dont want to wait untill Garbage Collecter runs.
I am using WPF.
After adding user controls dynamicaaly at runtime, How to Dispose that user control. Because there is no Dispose Method for User Controls. i dont want to wait untill Garbage Collecter runs.
Is there a reason you don't want to wait for the GC? If you remove the control from it's parent so there's no references to it then the GC will eat it, there's no need to dispose of it manually.
Even if you were able to dispose it, that wouldn't free memory. Dispose isn't about releasing memory - it's about releasing non-memory resources (e.g. GDI handles). I would assume that if the class doesn't implement IDisposable, it doesn't have any non-memory resources to release.
Thanks Steve.
Firstly i add numerous UsrCtr Elements on my Application Frequently. so if they are't removed instanteously, Huge memory will be wasted.
Actually i am using ForEach Loop at several places to iterate all the elements on the Application (WinForm) , so this loop still show the UserCtrs (After removing the UserCtr as chld)
Further more i also pass the refrence of the UserCtrs to several other places in my project through the Custom Defined Events and Delegate. So if add it at one Place on Main Form, still there may be possiblity that Refrence for the User Control Exists some other Place in the Project.
I think your design is flawed. If you still have a reference to something then you shouldn't try to free the memory anyway. Once something is actually no longer alive (no further references to it) it is effectively available memory in that, the next time you need some memory which cannot be supplied without a GC it will become free[1] and available.
Your design is not wasting memory, it is simply churning memory. This in itself may be problematic but it does not waste memory. If your design holds onto references to controls after they are no longer needed that wastes memory, but you cannot 'band-aid' over the problem by 'deleting' them. You need to tackle the root cause of passing these controls all about the place in a manner that makes their life cycle hard to control.
[1] barring finalizers which do not apply here