Garbage collection
Behavior of the garbage collector
A Java programmer does not have to worry about memory management, because it is automatically taken care of by the garbage collector. The Java virtual machine (JVM) decides when to run the garbage collector. The garbage collector is a low priority thread that runs periodically, releasing memory used by objects that are not needed anymore.
The garbage collection (GC) algorithm varies from one JVM to another. There are different algorithms being used, like reference