Remove instance
-
i have created a user control Dynamically and added it to panel as a child. now if i remoove it from panel but it still remains in memory. i want it to be permanently removed from memory. There is no Dispose method available. and i also tried by implementing idisposable interface and overwriting the Dispose() method but it still remain in memory. Is there any way to remove this object(User Control) from memory Regards Rishi
-
i have created a user control Dynamically and added it to panel as a child. now if i remoove it from panel but it still remains in memory. i want it to be permanently removed from memory. There is no Dispose method available. and i also tried by implementing idisposable interface and overwriting the Dispose() method but it still remain in memory. Is there any way to remove this object(User Control) from memory Regards Rishi
RishiKasnia wrote:
There is no Dispose method available.
AFAIK, UserControls inherited from IDisposable and have a Dispose() method.
I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog
-
RishiKasnia wrote:
There is no Dispose method available.
AFAIK, UserControls inherited from IDisposable and have a Dispose() method.
I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog
Thanks Pedram Behroozi You r right. Actually i was developing a Windows Appl Using WPF. in my case i need to remove this user control from memory . becoz after removing it from canvas or panel , if i try to fire some event then this Control also respond the event (Though it has been removed). for a workaround i detached the Event Handler for user control(when i remove it from canvas/panel). but it still resides in memory and remains throughout the program execution (becoz this control is not local to any Block) thus degrading performance . Regards Rishi
-
Thanks Pedram Behroozi You r right. Actually i was developing a Windows Appl Using WPF. in my case i need to remove this user control from memory . becoz after removing it from canvas or panel , if i try to fire some event then this Control also respond the event (Though it has been removed). for a workaround i detached the Event Handler for user control(when i remove it from canvas/panel). but it still resides in memory and remains throughout the program execution (becoz this control is not local to any Block) thus degrading performance . Regards Rishi
Well I know nothing about WPF UserControls and unfortunately I can't help you. I think it's better for you to ask it in WPF / WCF / WF Forum[^]. Regards
I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog
-
Well I know nothing about WPF UserControls and unfortunately I can't help you. I think it's better for you to ask it in WPF / WCF / WF Forum[^]. Regards
I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog
It ok Pedram Behroozi i have also posted the problem in WPF section but did't get any resonable ans. anyways thanks.
-
Thanks Pedram Behroozi You r right. Actually i was developing a Windows Appl Using WPF. in my case i need to remove this user control from memory . becoz after removing it from canvas or panel , if i try to fire some event then this Control also respond the event (Though it has been removed). for a workaround i detached the Event Handler for user control(when i remove it from canvas/panel). but it still resides in memory and remains throughout the program execution (becoz this control is not local to any Block) thus degrading performance . Regards Rishi
Removing instances from memory is the Garbage Collector's job. You don't have much control over when that gets done. But you can do cleanups inside the Dispose method. So basically you remove your event handlers in the Dispose method, and simply calle the Dispose method when you remove your control from the panel. (Typically Dispose is called for all the controls in a Form from within the Close method of the Form). Also if you control is hanging around in memory long after you have removed it, it could mean that you have some references to it somewhere in your program.
-
i have created a user control Dynamically and added it to panel as a child. now if i remoove it from panel but it still remains in memory. i want it to be permanently removed from memory. There is no Dispose method available. and i also tried by implementing idisposable interface and overwriting the Dispose() method but it still remain in memory. Is there any way to remove this object(User Control) from memory Regards Rishi
I didn't realise you were using WPF. WPF controls don't have Dispose methods because they don't need them. WPF controls don't use any unmanaged resources. To release the memory used by a WPF control all you have to do is make sure you have no references to it, and the garbage collector will automatically clear up all the memory next time it runs. Events are something to watch out for. Remember that the event source holds a reference to the event sink, so yes if you have a control sinking events from a source on a main form, then you will need to make sure you detach the event handlers if you want the control to be released. Note that the garbage collector does not immediately remove the object from memory. the GC only runs occasionally, and when there is a requirement for more memory than is available. This is normal behaviour and there is nothing wrong with it. if you want to test things you can call GC.Collect() to force a collection and see if your memory is cleared up, but you shouldn't leave this in the code when you release it. The GC is carefully balanced, and forcing unnecessary collections can really screw up it's performance. If you are still having problems getting the object to be cleared up, you can use WinDbg and SOS to peer deeper into the objects on the heap and work out why they aren't being collected. (Getting started with WinDbg & SOS[^], How to find a GC leak[^], Memory leak detection in .Net[^])
Simon
-
I didn't realise you were using WPF. WPF controls don't have Dispose methods because they don't need them. WPF controls don't use any unmanaged resources. To release the memory used by a WPF control all you have to do is make sure you have no references to it, and the garbage collector will automatically clear up all the memory next time it runs. Events are something to watch out for. Remember that the event source holds a reference to the event sink, so yes if you have a control sinking events from a source on a main form, then you will need to make sure you detach the event handlers if you want the control to be released. Note that the garbage collector does not immediately remove the object from memory. the GC only runs occasionally, and when there is a requirement for more memory than is available. This is normal behaviour and there is nothing wrong with it. if you want to test things you can call GC.Collect() to force a collection and see if your memory is cleared up, but you shouldn't leave this in the code when you release it. The GC is carefully balanced, and forcing unnecessary collections can really screw up it's performance. If you are still having problems getting the object to be cleared up, you can use WinDbg and SOS to peer deeper into the objects on the heap and work out why they aren't being collected. (Getting started with WinDbg & SOS[^], How to find a GC leak[^], Memory leak detection in .Net[^])
Simon
Thanks Simon :)