The Java language is the most widely used programming language to rely on garbage collection. The name "garbage collection" implies that objects that are no longer needed by the program are "garbage" and can be thrown away or destroyed. The Java virtual machine's heap stores all objects created by a running Java application. Objects are created by the
operator but never freed explicitly by the code.
Garbage collection is the process of automatically freeing objects that are no longer referenced by the program. Such as object must be recycled so that the space is available for succeeding new objects. The garbage collector determines which objects are no longer referenced by the program and make available the heap space occupied by such unreferenced objects. Garbage collection relieves the programmer from the load of freeing allocated memory.
Read example at:
Recommend the tutorial