The case for erasing references hasn't changed much between VB6 and the
latest version of .NET.... keeping an object reachable longer than needed
will still leak memory. Ok, gc handles the case of circular references,
sort of -- you get random cleanup order so they're still a bad idea.
I'm sorry, but that is absolutely wrong.
There is a difference between an object remaining in memory longer than is
absolutely necessary and an object remaining in memory for the duration of
the application's lifetime because it was never explicitly de-referenced.
In the former, memory may be taken up longer than is needed, but the object
can still sit there causing no harm, if it has been disposed (when needed),
which .NET offers the Using statement so that this can be done
automatically. In the latter, not only is memory taken up, but external
resources are tied up as well, because the class's terminate method is not
called.
In .NET, you are likely to adversely affect the performance of your
application by explicitly dereferencing your object (x = Nothing)! The GC
is optimized to look at running methods when collecting and to determine if
objects that still have application roots are actually going to be used in
the remainder of the method running. If you were to be cleaining up your
objects by setting them to Nothing, but hadn't reached that point of the
code yet, the GC would actually NOT mark your object that isn't going to be
used for any meaningful purpose for collection, now that you've got another
reference to it (the clean up code) later in the code.
In short, there is really no compelling reason to set object variables to
Nothing in .NET. The only thing you gain (and it's not even certain that
you will gain this) is that the object *may* become elligible for collection
sooner than when its variable falls out of scope, but as I've pointed out,
you could actually cause that to take even longer by setting the variable to
Nothing!
-Scott