It is good habit. The sooner you release all references to an object, the sooner the GC will be able to collect it.
When a function exits, all locally declared variables are effectively nulled (they are owned by the stack frame itself, which no longer counts as a GC root). So even if you forget to null local variables, they will eventually get cleaned up. The catch is that if a GC cycle runs during that window of time after the variable is no longer needed, but before the stack frame unwinds (such as if you are continuing to do work in the rest of the method), then the object will not be seen as "collectible" by the GC. This means that it will be promoted to the next higher garbage generation, which can be a bad thing if it happens a lot.
Now when it comes to class member fields, then yes you should always be nulling them once they are no longer needed. Otherwise, they will remain in the GC heap at least until the owning class itself gets collected (even longer if the objects have a Finalizer).
You can get some fabulous information on this topic from Rico Mariani's weblog at http://blogs.msdn.com/ricom/ (he calls this particular phenomenon "mid-life crisis").
HTH
|