Someone suggested to me recently that to avoid hogging the memory, a Java application should always destroy all objects created in the method at the end of this method whenever it’s possible (by setting it to null).
I haven’t seen that done too often before, and from my point of you it seems to defeat the purpose of garbage collector in the first place. Are there any guidelines / reasoning when this strategy is useful? Would you ever face an application when this is necessary if you are careful about your objects?
Setting references to null doesn’t destroy the objects. It just eliminates one reference to them. In order for the memory to be freed, the garbage collector is still required. For locally scoped variables, this makes no difference at all.