Does setting object reference to null help the GC in collection

S

Shree

I am having this confusion going on in my head for sometime now. The
question is whether setting the object reference to null (nothing)
help the GC in collection in anyway?
Can anyone shed some light on this? Is there any way to validate this
proposition? Or is it just a myth...

-Thanks,
Shree
 
M

Marc Gravell

It depends on the context ;-p

If you mean a method variable in a short duration method, then no, not
really. In fact, a check like "if (foo!=null) {foo=null;}" could /
extend/ the theoretical lifetime by making it last at least until the
check (rather than the last genuine time the variable is read).

However: If you mean a field on a class that is going to stay alive,
then: definitely. But if that class instance is going out of scope
anyway, no again...

Clear as mud?

Marc
 
P

Peter Morris

I believe not, take this example

object x = new Person();
x.FirstName = "Peter";
x.LastName = "Morris";

//Position A

...lots of code here..

//Position B
x.FullName = x.FirstName + " " + x.LastName;

//Position C

...lots of code here..


The first thing to remember is that the GC can interrupt this method at any
point. If it interrupts at position A it will not collect the value in "x"
because the value is referenced later in the method. If it interupts at
position C it will be able to collect the value in "x" because it is no
longer used.

In summary

The object is only held onto in this example if the variable is referenced.
The fact that the object is referenced by a variable is irrelevant if that
variable is not itself referenced.



Pete
 
B

Brian Gideon

I believe not, take this example

object x = new Person();
x.FirstName = "Peter";
x.LastName = "Morris";

//Position A

..lots of code here..

//Position B
x.FullName = x.FirstName + " " + x.LastName;

//Position C

..lots of code here..

The first thing to remember is that the GC can interrupt this method at any
point.  If it interrupts at position A it will not collect the value in "x"
because the value is referenced later in the method.  If it interupts at
position C it will be able to collect the value in "x" because it is no
longer used.

In summary

The object is only held onto in this example if the variable is referenced..
The fact that the object is referenced by a variable is irrelevant if that
variable is not itself referenced.

Pete

Strangley, it can be collected in position B halfway through the call
to FullName as long as it's after any explicit or implicit use of the
'this' pointer inside that property.

Here is a link that explains why this might be important to you and
gives a example of where you would use GC.KeepAlive.

http://blogs.msdn.com/cbrumme/archive/2003/04/19/51365.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top