The JVM's heap stores all the objects generating by a running Java program. Java uses the new operator to create objects and memory for new objects is allocated on the heap at run time. Garbage collection is the mechanism of automatically freeing up the memory contained by the objects that are no longer referenced by the program. This keeps the programmer free from having to keep track of when to free allocated memory, thereby preventing many potential bugs and headaches.
The mechanism "garbage collection" is implemented through an algorithm to the objects that are no longer needed by the program and can be thrown away. "memory recycling." might be the more appropriate and up-to-date metaphor if an object is no longer referenced by the program, the heap space it occupies must be recycled so that the space is available for subsequent new objects. The garbage collector uses a mechanism to determine which objects are no longer referenced by the program and make available the heap space occupied by such unreferenced objects. Before freeing the memory occupied by the unreferenced objects, the garbage collector must run any finalizers of objects being freed.
A garbage collector may also perform a task of heap fragmentation while freeing up the unreferenced objects. Heap fragmentation is done through normal program execution.
If you enjoyed this post then why not add us on Google+? Add us to your Circles
Liked it! Share this Tutorial