setting variables to null

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello All,
From time to time, I noticed from sample snippets of code that the author
explicitly set their variables to null to towards the end of a method.

Would someone tell me the reasoning for this. is it for optimization
reasons (eg. garbage collection)
thanks
 
From time to time, I noticed from sample snippets of code that the author
explicitly set their variables to null to towards the end of a method.

Would someone tell me the reasoning for this. is it for optimization
reasons (eg. garbage collection)

It's usually because the authors think they know better than the JIT
authors - and they're almost always wrong. People like to do it "just
in case" despite the fact that if they really needed to do it, huge
amounts of other code wouldn't work.

Basically, you don't need to do this except in very *very* occasional
circumstances, where you know that you won't be using the value of the
variable again for the rest of a significantly long-running method, but
the JIT won't be able to spot it because you've got a read that's
conditional on something that just won't happen. (eg a variable that is
only read the first time through a loop.)
 
Back
Top