Re: Difference between setting reference=null and going out of scope?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Adam Clauss (cabadam_at_tamu.edu)
Date: 06/24/04


Date: Thu, 24 Jun 2004 11:53:22 -0500

Interesting, alright, thanks for the info.

-- 
Adam Clauss
cabadam@tamu.edu
"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message news:MPG.1b4508636574553d98ad93@msnews.microsoft.com...
> Adam Clauss <cabadam@tamu.edu> wrote:
> > Basically, my question is in terms of performance and the garbage collector -
> > Is there any difference between
> > a) letting a variable simply go out of scope
> > b) explicity setting it to null once I am done with it
>
> Yes - explicitly setting it to null might take slightly longer, if the
> JIT doesn't notice that the write is useless.
>
> Actually, when running under a debugger there'll be a difference in
> that the object (assuming there are no other references to it) will be
> eligible for garbage collection from the point at which the variable is
> set to null, compared with having to actually wait for it to go out of
> scope. In release mode, it doesn't even have to wait to go out of scope
> or be set to null - after the last use of it, the GC won't treat it as
> a live reference.
>
> > I have no particular reason to think that b) would gain me anything,
> > but I thought maybe it would be a sure sign to the garbage
> > collector that Yes, you can go ahead and remove the object.
>
> Nope.
>
> -- 
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too


Relevant Pages

  • Re: Difference between setting reference=null and going out of scope?
    ... my question is in terms of performance and the garbage collector - ... > a) letting a variable simply go out of scope ... > b) explicity setting it to null once I am done with it ... a live reference. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: general performance question
    ... This has been discussed on this group, and the consensus is that the method's stack frame continues to point to the LargeObject, so that it can't be collected until the method returns. ... (It seems to me that the JVM should be free to null out the reference once it goes out of scope, or even if it's in scope but flow analysis makes it clear that it can't be used any more, but that was a minority opinion.) ... Unfortunately, programmers often take this advice too far, using explicit nulling in the hope of helping the garbage collector. ... But in most cases, it doesn't help the garbage collector at all, and in some cases, it can actually hurt your program's performance. ...
    (comp.lang.java.programmer)
  • Re: Petition for the removal or voluntary departure of Richard Heathfield from this newsgroup
    ... when A goes out of scope or is reassigned In this case, ... linked list when Root goes out of scope (or perhaps is just assigned ... AFAIK a classic garbage collector periodically stops the app and goes ... running around looking for pointers to heap that are fair game to get ...
    (comp.programming)
  • Re: Local variables - quick question
    ... You can't use a variable which isn't in scope within ... possiblity to declare class argument int i - only signal for me is compiler ... > being alive by the garbage collector. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Garbage collector quiz
    ... >> the out of scope variable tmp prevent the garbage collector from ... > variable count as a VALID reference? ...
    (comp.lang.java.programmer)