
Signature
Sean Hederman
http://codingsanity.blogspot.com
> 2) Setting the object to nothing does help the GC clean up the object
> sooner. In fact if you never set it to nothing, the object will never be
> collected.
This is simply wrong. For local variables, setting the variable to
nothing usually makes no difference at all, as the JIT can tell when it
is last read. For instance variables, you only need to set the variable
to nothing if you expect the containing object to live longer than the
referenced object needs to.
> GC can only occur on an object when all references to it are
> gone.
Indeed - but that doesn't require anything to be set to nothing.
Setting variables' values to nothing (you can never set an *object* to
nothing) unnecessarily clutters up code, reducing readability.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Teresa - 14 Jan 2005 14:29 GMT
Hi Jon - I'm not sure I follow you discussion to the "set nothing" topic.
I've acutally tried and succeeded in setting an object to nothing, the watch
(in debug mode) showed that the object equals to nothing. So is my last line
of code not neccessary?
Dim mycls as MyClass
' do something with mycls obj
mycls.dipose()
mycls = Nothing
> > 2) Setting the object to nothing does help the GC clean up the object
> > sooner. In fact if you never set it to nothing, the object will never be
[quoted text clipped - 13 lines]
> Setting variables' values to nothing (you can never set an *object* to
> nothing) unnecessarily clutters up code, reducing readability.
Sean Hederman - 14 Jan 2005 16:19 GMT
Jon's right, sorry for not being clear enough. As he said, the only time you
need to specifically set the reference to nothing is if you want it to be
garbage collected before the containing object. As an example, consider a
service which every hour or so creates an object which it uses and then
discards fairly quickly. It could then make sense to ensure that the object
is available for garbage collection after you're finished with it,
especially if it's a big object. In such a scenario, you would set the
reference to Nothing.
However, most objects lifetimes are tied to their parent, and/or their
scope. For these, it is not neccesary to set the reference to Nothing, since
the child objects will be available for garbage collection once their
references go out of scope, or their parent is collected. As Jon noted,
references may actually be cleared before they go out of scope, if the JIT
figures out that they're not used anymore.
So, to answer your specific question, you would only need to consider
setting mycls to Nothing if the object that contained it would survive
significantly longer than mycls, AND mycls was not going out of scope.
> Hi Jon - I'm not sure I follow you discussion to the "set nothing" topic.
> I've acutally tried and succeeded in setting an object to nothing, the
[quoted text clipped - 26 lines]
>> Setting variables' values to nothing (you can never set an *object* to
>> nothing) unnecessarily clutters up code, reducing readability.