There seems to be a useful (hopefully) discussion going on where lots of
learning is occurring so I have continued to participate in this thread.



> If you're talking about the local variable of a method, you are correct.

> However, if you are talking about class member that's an object reference,

> setting the reference to Nothing will allow the referenced object to be

> collected if your class instance was the only reference to it, even if

> your class instance is still alive (as far as the GC goes).



We have agreed on several things here, so perhaps we are merely dealing with
subtle semantics and definitions on this item; let me offer the following
clarification in case I have not communicated clearly.



References do not impact garbage collection of managed objects. When the
garbage collector runs it assumes all memory in the heap is garbage and it
then walks its list of "roots" to build a graph of reachable objects. A root
is a pointer to a reference type. When a root can access another object,
that object is added to the graph of reachable objects. All globals,
statics, locals and CPU registers are considered roots. Once all the roots
have been walked the garbage collector knows which objects are reachable
from the application's roots. This process occurs every time the garbage
collector begins its charge. (Reference: p.457 of Applied Microsoft .NET
Framework Programming by Jeffrey Richter).



An instance of an object cannot be "still alive" and also collected unless
we are talking about "Weak References" (in which case we almost certainly
need to specify, as I bet that we loose half of our audience here. See: p.
485-489 in Richter's book). Assuming strong references, if an object is
still reachable by code, then the Garbage Collector will leave it alone
because the object will appear in the object graph after walking the roots.
Code that sets an object to a value are seen as reachable (e.g. Set object =
nothing) causing an entry to be made in the graph.



*       You might question the need for such an object to be a class member,

*       but it could be that the object is only "in use" when the instance
is

*       in a particular state.  Setting the reference to Nothing when the

*       object isn't needed any more is clearly useful in that case.



True on both counts. I would question the need for the apparent
excessiveness of the object's scope, but assuming that the scope was
required for a legitimate reason (and I can think of a few), setting it to
Nothing would allow the memory on the managed heap to no longer be reachable
and would therefore qualify it to be reclaimed (assuming that it wasn't on
the freachable queue. (yet another source of "roots")). See p. 469-471.



--Paul Mehner









-----Original Message-----
From: Unmoderated discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of J. Merrill
Sent: Wednesday, January 26, 2005 12:15 PM
To: [email protected]
Subject: Re: [ADVANCED-DOTNET] Disposing of objects



At 11:02 AM 1/26/2005, Paul Mehner wrote (in part)

>Many developers are also under the mistaken impression that setting an

>object to Nothing will destroy it or somehow promote a faster clean up by

>the garbage collector -- or feel that it is necessary or somehow desirable

>because that the way it was done in VB6. Often former VB6 developers place

>extraneous "set object = nothing" statements near the end of every single

>routine as if they were laying non-effective sacrifices to the ancient VB6

>god. All this effort buys them nothing because it does not make the
object's

>clean-up code to execute in a deterministic fashion and it seldom has any

>impact on the performance of garbage collection.







But to reiterate my agreement with your basic statement, adding (perhaps
more than one) "localvar = nothing" to the bottom of a routine (as you were
apparently encouraged to do in VB6) is a waste of time -- and I'm sure
there's at least one routine out there that has a try/finally only so that
the local object references can be set to Nothing in the Finally part.











J. Merrill / Analytical Software Corp



===================================

This list is hosted by DevelopMentor.  http://www.develop.com



View archives and manage your subscription(s) at http://discuss.develop.com


===================================
This list is hosted by DevelopMentor�  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to