How Does the Garbage Collector Work?

The Java garbage collector helps recycle memory and other resources when they are no longer needed. It consists of a separate low-priority thread of execution that executes in the background and keeps track of all the objects that are used in a Java program. When an object is no longer needed, it becomes subject to garbage collection.

At this point, you are probably wondering what it means for an object to be no longer needed and how the garbage collector determines which objects are needed and which are not. A program does not need an object that is no longer in use or that cannot be brought back into use. The garbage collector determines that an object cannot be used if it is no longer reachable by the program. ...

Get Sun Certification Training Guide (310-025, 310-027): Java™ 2 Programmer and Developer Exams 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.