What is Garbage Collection in java?


Since objects are dynamically allocated by using the new operator, such objects must be
destroyed and their memory released for later reallocation.

In some languages, such as C++, dynamically allocated objects must be manually released by
use of a delete operator.

Java takes a different approach; it handles deallocation automatically. The technique that
accomplishes this is called garbage collection.

It works like this: when no references to an object exist, that object is assumed to be no
longer needed, and the memory occupied by the object can be reclaimed. 

There is no explicitneed to destroy objects as in C++. Garbage collection only occurs 
sporadically (if at all) during the execution of your program. It will not occur simply because one or more objects exist that are no longer used.

Comments