Controls aren't garbage collected

  • Thread starter Thread starter TPS
  • Start date Start date
T

TPS

I've searched high and low, but can't seem to come across a solution
for garbage collection of my dynamically declared controls.

For testing I've created a small example:

I have two classes:

public class Foo
{
public Foo()
{
Debug.WriteLine("Foo ctor");
}
~Foo()
{
Debug.WriteLine("Desctructing Foo");
}
}

public class FooControl : Control
{
public FooControl()
{
Debug.WriteLine("FooControl ctor");
}
~FooControl()
{
Debug.WriteLine("Desctructing FooControl");
}
}

And I use them like this:

private void Form1_Load(object sender, EventArgs e)
{
List<Foo> list = new List<Foo>();
list.Add(new Foo());
list.Add(new Foo());

List<FooControl> list2 = new List<FooControl>();
list2.Add(new FooControl());
list2.Add(new FooControl());
}

This causes the output:

Foo ctor
Foo ctor
FooControl ctor
FooControl ctor

Which is correct...
Then I explicitly call for garbage collection:

GC.Collect();

Now this is output:

Desctructing Foo
Desctructing Foo

Not until I close the program using Application.Exit(); are the
FooControl objects destroyed, and outputs:

Desctructing FooControl
Desctructing FooControl
....

I've tried to create a regular .Net Framework Windows application with
exactly the same code, and here the FooControl objects are destroyed
correctly during garbage collection.

What can I do to make sure that my no longer referenced controls are
properly destroyed.

TPS.
 
Hi Fabien

That doesn't change anything, it is merely a method to allow you to do
some cleanup before the object is destroyed. Calling it will initiate
the cleanup but it wont destroy the object.

TPS.

Fabien skrev:
 
Hi Alex...

Hasn't "the time come" when you explicitly call the GC.Collect()
method?
When I do in my example, the two Foo objects are destroyed, but not the
two FooControl objects although they are declared in the same scope.

TPS


Alex Yakhnin [MVP] skrev:
 
Have you read the blogs? GC.Collect does not force finalizers to run.


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--

If your app is windowed try having it lose focus (bring another app forward)
and see what happens.

Unless you are actually having an OOM problem, you shouldn't be concerend
about when the objects are collected or finalized.



TPS said:
Hi Alex...

Hasn't "the time come" when you explicitly call the GC.Collect()
method?
When I do in my example, the two Foo objects are destroyed, but not the
two FooControl objects although they are declared in the same scope.

TPS


Alex Yakhnin [MVP] skrev:
 
The thing that makes me wonder is why does the garbage collector in
compact framework V2.0 collect the two objects the that isn't inherited
from Control, but doesn't collect the two objects that is inherited
from Control?
The garbage collector in the full framework V2.0 collects all four
immediately!

And I do have a OOM problem, in the project I'm working on there is a
custom made ListBox consisting of dynamic list of ListBoxItems
(inheritet from Control). The list changes every 5 sec, and the device
has to run for 5 - 10 years, so sooner or later it becomes a problem
that the ListBoxItems aren't finalized.
I'm not calling GC.Collect() in my code, but none of the ListBoxItems
are collected after a week and the memory usage is getting close to 95%
now.

TPS
 
Back
Top