In this case, you won't get any benefit unless a collection occurs after the
variable is set to null and before the stack collapses after the method
returns to the caller. Since refVar is about to be evicted from the stack,
there's really no benefit to setting it to null. In other cases where the
time delta is greater, a live local reference to an object on the heap will
cause the object to be tracable, and therefore not eligible for collection.
However, in most cases this is a micro optimization, and won't buy you a
lot - especially if you're talking about local variables in methods that are
reasonably-sized.
Note also that setting a reference to null is not the same thing as
Dispose'ing it.
--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com
"Pekus Cons. e Desenvolvimento" <pekus@REMOVE_pekus.com.br> wrote in message
news:ez****************@TK2MSFTNGP12.phx.gbl...
Hi,
I have seen some developers commenting that a reference variable is 'more
garbage collection eligible' if the programmer set its value to null after
using it, like this:
public void MemberFunction()
{
AnyClass refVar = new AnyClass ();
// Normal use of refVar functionalities
refVar = null;
}
Is it real? Any comment about?
Thanks,
Baccarin.