Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - memory managment

Reply
 
Old 06-11-2008   #1 (permalink)
Fred


 
 

memory managment

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)


--
Fred
foleide@xxxxxx


My System SpecsSystem Spec
Old 06-11-2008   #2 (permalink)
Göran Andersson


 
 

Re: memory managment

Fred wrote:
Quote:

> 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.

--
Göran Andersson
_____
http://www.guffa.com
My System SpecsSystem Spec
Old 06-11-2008   #3 (permalink)
Jack Jackson


 
 

Re: memory managment

On Wed, 11 Jun 2008 07:32:30 +0200, "Fred" <foleide@xxxxxx>
wrote:
Quote:

> 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).
Quote:

>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.
My System SpecsSystem Spec
Old 06-12-2008   #4 (permalink)
Fred


 
 

Re: memory managment

"Jack Jackson" <jjackson@xxxxxx> a écrit dans le message de
news:19l05498ecef7m1ul19q2q3scil57bhgm6@xxxxxx
Quote:

> On Wed, 11 Jun 2008 07:32:30 +0200, "Fred" <foleide@xxxxxx>
> wrote:
>
Quote:

>> 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).
>
Quote:

>>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.

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 ?

--
Fred
foleide@xxxxxx

My System SpecsSystem Spec
Old 06-12-2008   #5 (permalink)
Jack Jackson


 
 

Re: memory managment

On Thu, 12 Jun 2008 13:15:03 +0200, "Fred" <foleide@xxxxxx>
wrote:
Quote:

>"Jack Jackson" <jjackson@xxxxxx> a écrit dans le message de
>news:19l05498ecef7m1ul19q2q3scil57bhgm6@xxxxxx
Quote:

>> On Wed, 11 Jun 2008 07:32:30 +0200, "Fred" <foleide@xxxxxx>
>> wrote:
>>
Quote:

>>> 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).
>>
Quote:

>>>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.
>
>
>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.
My System SpecsSystem Spec
Old 06-12-2008   #6 (permalink)
Göran Andersson


 
 

Re: memory managment

Fred wrote:
Quote:

> 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.
Quote:

> 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.
Quote:

> 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.

--
Göran Andersson
_____
http://www.guffa.com
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Blue Screen memory managment Live Messenger
Process managment PowerShell
Vista good memory managment, could it be true? Vista General
DVD-drive power managment Vista hardware & devices
Disk Managment Vista performance & maintenance


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46