Always remove short-lived objects from long-lived container objects when the task is over. For example, objects attached to a java.nio.channels.SelectionKey
object must be removed when they are no longer needed. Doing so reduces the likelihood of memory leaks. Similarly, use of array-based data structures such as ArrayLists
can introduce a requirement to indicate the absence of an entry by explicitly setting its individual array element to null
.
This guideline specifically addresses objects in containers. For an instance where nulling out objects does not aid garbage collection, see OBJ54-JG. Do not attempt to help the garbage collector by setting local reference variables to null.
Noncompliant Code Example (Removing Short-Lived Objects)
...