Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Destroy controls

Tags:

c#

wpf

I use a for loop to generate some controls. Each control presents a special visualisation. I can not use controls like ListBox or somethink like that. I must use my approach. The generated controls are hosted in a Canvas. After a while, some controls are not longer needed. Some controls must be removed. I can manually remove a controls via

c.Children.Remove( ... );

this line. How do I determine that the controls was really collected from be garbage collection? Maybe the control already exists in the memory ... . How do I ensure this?

The problem is that I generate data in tons!

Thanks in advance! :-)

like image 730
System.Data Avatar asked Dec 17 '22 20:12

System.Data


1 Answers

So if you call to Destroy the control, and you haven't maintained any other references to the control in memory (like in a hashtable of controls or something), then the GC will collect it.

Don't try and force the .NET GC to do something fancy, let it do its work in its own time as the library authors designed it. If you Destroy it, it will be gone, in due time.

Lastly, if you're concerned that it's still consuming memory, it will be time to start profiling your code, like for instance with RedGate Ants.

like image 141
jcolebrand Avatar answered Jan 05 '23 14:01

jcolebrand