Garbage Collection

Automatic garbage collection is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting the unused objects. An in use object, or a referenced object, means that some part of your program still maintains a pointer to that object. An unused object, or unreferenced object, is no longer referenced by any part of your program. So the memory used by an unreferenced object can be reclaimed.

In a programming language like C, allocating and deallocating memory is a manual process. In Java, process of deallocating memory is handled automatically by the garbage collector.

Note, when considering the memory used by objects, which can contain references to other objects, which in turn can reference still other objects, and so on, with the distinct possibility of circular references among these -- identifying which objects are actually "orphans" with no references to them is an interesting problem in its own right, as the diagram below suggests.