Whenever you have a reference to an object stored in a variable, that object cannot be garbage-collected. If your reference is in a local variable in a short-duration method, it will go out of scope quickly and be available for garbage collection. Therefore, no need to set reference to null. In this case, setting it to null is a style thing, as the previous posting suggested. I think it's good practice, but can't claim that I actually always do it! If the variable containing your reference is potentially long-lived (member data of long-lived object or, worse, class static data), then garbage collection could be prevented for a long time or for ever. In such cases, you should set references to null when they are no longer needed, to facilitate garbage collection. In the worst case, failure to do this can result in a "memory leak". Extra care has to be taken with arrays and collections. If an array or collection holds a reference to an object, that will prevent garbage collection. Long-lived arrays or collections could therefore cause memory leaks if not used carefully.
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Joined: Oct 29, 2001
Hi Christopher and Peter, Thank you for your opinions, really useful. Samuel