Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: removed ... to make examples compile and some text tweaks

...

Code Block
bgColor#FFCCCC
{ // Local scope
...
  int[] buffer = new int[100];
  doSomething(buffer);
  buffer = null  // No need to explicitly assign null
...
} 

Compliant Solution

Program logic occasionally requires tight control over the lifetime of an object referenced from a local variable. In the unusual cases where such control is necessary, use a lexical block to limit the scope of the variable because the garbage collector can collect the object immediately when it goes out of scope [Bloch 2008].

...

It is unnecessary to set local reference variables to null when they are no longer needed in a mistaken attempt to help the garbage collector recover their spacereclaim the associated memory.

Bibliography

[Bloch 2008]

Item 6, "Eliminate Obsolete Object References"

...