Reference variable x GC

  • Thread starter Thread starter Pekus Cons. e Desenvolvimento
  • Start date Start date
P

Pekus Cons. e Desenvolvimento

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.
 
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.
 
Pekus Cons. e Desenvolvimento said:
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, [...]
Is it real? Any comment about?

References that are still in use can be null for many good reasons, so I'd
hope this isn't the case.

In fact, I suspect that setting the value of a variable at any point says to
the GC, "I am still changing this variable, so I obviously haven't finished
with it."

This is just speculation, though.

P.
 
Pekus said:
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;
}

For local variables, the JIT compiler determines where the last use of
that variable is in the method. The JIT marks the reference as dead
after that point, so the GC can collect whatever is referenced by that
local (as long as no other live reference to the object exists).

In other words, in general, setting a reference to null does not help
anything when the reference is maintained in a local variable.
 
Paul E Collins said:
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, [...]
Is it real? Any comment about?

References that are still in use can be null for many good reasons, so I'd
hope this isn't the case.

I think there's really a mismatch of terminology here. Variables
themselves aren't garbage collected - objects are. Setting the value of
a variable to null can, in *some* situations, shorten the lifetime of
an object. It can never shorten the lifetime of an actual object.
Usually it's not worth doing though, as the JIT compiler can usually
see when a variable is no longer used even if it is still theoretically
"live".
 
Pekus Cons. e Desenvolvimento said:
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....

It's not so much a problem with local variables in a function. What you have
to watch out for is member variables in a class. If you don't set the member
variable to null if it's not needed any more, but the instance of the class
is still being referenced, the memory consumed by the member variable won't
be garbage-collected until the parent object is collectible. In that
situation you would want to be sure to set the member variable to null.
 
Bret Mulvey said:
It's not so much a problem with local variables in a function. What you have
to watch out for is member variables in a class. If you don't set the member
variable to null if it's not needed any more, but the instance of the class
is still being referenced, the memory consumed by the member variable won't
be garbage-collected until the parent object is collectible. In that
situation you would want to be sure to set the member variable to null.

Do you run into situations frequently? I very rarely end up in the
situation where I know that I don't need *part* of the state of an
object any more, but that the rest of the object is still required.
When I come across situations like that I usually find that a redesign
of the class gets rid of the problem and ends up with a cleaner
solution anywa6y.
 
Back
Top