Freeing Unused Variables and Resources

The Java garbage collection mechanism usually frees unused objects automatically. “Unused” from the point of view of the garbage collector means that the objects are not referenced from somewhere else. If objects are no longer needed in a program, but are still referenced by a variable or indirectly by another object, the garbage collector can't determine that the object can be removed, and the corresponding memory is not reclaimed. Thus, if an object is allocated and then no longer needed, but the variable holding the object is still in the valid scope, it may make sense to set the variable to null explicitly. For example, if a buffer is allocated at the beginning of a method, but then no longer needed ...

Get Java™ 2 Micro Edition Application Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.