memory managment

F

Fred

Hello,

When a control is disposed, should it first set its Tag property (if
present) to null ?

I use a commercial control and I saw (with a .NET memory profiler) that
the objects I put in this property were not collected even when the
control is disposed (until I set the tag
property to null)
 
G

Göran Andersson

Fred said:
Hello,

When a control is disposed, should it first set its Tag property (if
present) to null ?

I use a commercial control and I saw (with a .NET memory profiler) that
the objects I put in this property were not collected even when the
control is disposed (until I set the tag
property to null)

If you hang on to the control a long time after it has been disposed,
you can set the Tag property to null to make thae object that it
references collectable.

Otherwise there is rarely any reason to setting references to null. When
the control gets collectable, any objects that it references (and isn't
references from somewhere else) also automatically gets collectable.
 
J

Jack Jackson

Hello,

When a control is disposed, should it first set its Tag property (if
present) to null ?

If the control will soon be eligible for garbage collection, then no.
If the control won't be eligible for garbage collection soon, then
setting the Tag property to null will allow the object references by
Tag to be collected sooner (assuming there are no other references to
that object).
I use a commercial control and I saw (with a .NET memory profiler) that
the objects I put in this property were not collected even when the
control is disposed (until I set the tag
property to null)

Calling Dispose has no effect on garbage collection. An object is
eligible for garbage collection when no live object has a reference it
regardless of whether or not Dispose has been called. Dispose simply
frees any resources that the object is holding. For UI controls
Dispose releases the associated Windows control. In no case does
Dispose cause the object to be garbage collected. If Dispose isn't
called explicitly, it will be called when the object is garbage
collected. Explicitly calling Dispose is simply a way to free
resources in a deterministic way, since the object may not be garbage
collected for a very long time.

If you aren't holding any references to the control (it is removed
from its container collection, any event handlers it added for other
objects have been removed, you don't have any variables that hold
reference to it, etc.) then it is eligible for garbage collection and
so is the object that its Tab property references.
 
F

Fred

Jack Jackson said:
If the control will soon be eligible for garbage collection, then no.
If the control won't be eligible for garbage collection soon, then
setting the Tag property to null will allow the object references by
Tag to be collected sooner (assuming there are no other references to
that object).


Calling Dispose has no effect on garbage collection. An object is
eligible for garbage collection when no live object has a reference it
regardless of whether or not Dispose has been called. Dispose simply
frees any resources that the object is holding. For UI controls
Dispose releases the associated Windows control. In no case does
Dispose cause the object to be garbage collected. If Dispose isn't
called explicitly, it will be called when the object is garbage
collected. Explicitly calling Dispose is simply a way to free
resources in a deterministic way, since the object may not be garbage
collected for a very long time.

If you aren't holding any references to the control (it is removed
from its container collection, any event handlers it added for other
objects have been removed, you don't have any variables that hold
reference to it, etc.) then it is eligible for garbage collection and
so is the object that its Tab property references.


Thank you Göran and Jack,

I omitted to say that I was calling GC.Collect in my tests before to
make a memory snapshot. I know that Dispose won't cause the object to be
garbage collected immediatly.

So :
1) Not settings the Tag property to null, I can see in the memory the
disposed control and some of my classes instances (after GC.Collect).
2) Setting the Tag proprty to null, every thing disappear (after
GC.Collect)

According to your explainations, it means, I think, that there were
somewhere some references to the control in my classes (I will have a
look and correct this). A kind of circular reference that I broke
setting the Tag property to null and permit the garbage collector to do
its job ?

So, about my question. Isn't it a best practice, if I develop a custom
control, to set all it's possible extern references (as the Tag
property) to null when disposing ?
 
J

Jack Jackson

Thank you Göran and Jack,

I omitted to say that I was calling GC.Collect in my tests before to
make a memory snapshot. I know that Dispose won't cause the object to be
garbage collected immediatly.

So :
1) Not settings the Tag property to null, I can see in the memory the
disposed control and some of my classes instances (after GC.Collect).
2) Setting the Tag proprty to null, every thing disappear (after
GC.Collect)

According to your explainations, it means, I think, that there were
somewhere some references to the control in my classes (I will have a
look and correct this). A kind of circular reference that I broke
setting the Tag property to null and permit the garbage collector to do
its job ?

So, about my question. Isn't it a best practice, if I develop a custom
control, to set all it's possible extern references (as the Tag
property) to null when disposing ?

If you forced a garbage collection and the control did not get
collected, then yes that implies that something still references it.
There are some tools that will give you information about what is
keeping objects alive.

It certainly won't hurt to null out references, but in general it
should not be necessary. It might help to hide other problems, which
could be either a good thing or a bad thing depending on your point of
view.
 
G

Göran Andersson

Fred said:
I omitted to say that I was calling GC.Collect in my tests before to
make a memory snapshot. I know that Dispose won't cause the object to be
garbage collected immediatly.

So :
1) Not settings the Tag property to null, I can see in the memory the
disposed control and some of my classes instances (after GC.Collect).
2) Setting the Tag proprty to null, every thing disappear (after
GC.Collect)

Calling GC.Collect doesn't ensure that all collectable objects are
collected. The GC still decides how extensive the collection should be,
and if it's worth it at all. If there is too little that would be
collected, it might decide to skip the collection altogether. So,
calling GC.Collect can not really be used as a diagnostic tool.
According to your explainations, it means, I think, that there were
somewhere some references to the control in my classes (I will have a
look and correct this). A kind of circular reference that I broke
setting the Tag property to null and permit the garbage collector to do
its job ?

Circular references is not a problem at all for the GC. It looks for
active references, so two objects that reference each other but are not
referenced from somewhere else, are still recognised as unreachable.
So, about my question. Isn't it a best practice, if I develop a custom
control, to set all it's possible extern references (as the Tag
property) to null when disposing ?

That depends on the situation. In most cases it's not worth the trouble,
as the referene will soon be unreachable anyway.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top