Garbage Collection in Java
Garbage collection" means objects that are no longer required by the program are "garbage" and can be thrown away.
JVM?s heap stores all objects that are created by a running Java application. GC is the process that automatically freeing objects that are no longer referenced by the java program. JVM specification does not require any particular garbage collection technique. It doesn't even require garbage collection at all. More accurate and up-to-date definition of GC is "memory recycling."
When an object is no longer required by the program, the JVM heap space it occupies can be recycled ,so that the new space is made available for newly created objects. GC somehow determine which objects are no longer referenced by the program and make available the heap space occupied by such unreferenced objects. The process of freeing unreferenced objects, the GC must run any finalizers of objects for freeing objects. Heap fragmentation occurs through normal program execution. In heap fragmentation new objects are allocated, and unreferenced objects are freed so that free portions of heap memory can be used for occupying by live objects.
Requests for memory allocation by new objects is filled by extending the size of the heap even though there is enough total unused space in the existing heap. GC frees us from the burden of freeing allocated memory. GC know when to explicitly free allocated memory can be made available for new objects.
GC has the following advantages such as :-
(1) GC makes us more productive.Programming in non-garbage-collected languages face an elusive memory problem. Programming in Java using GC can avoid the problem of memory & scheduling .
(2) GC helps ensure program integrity. GC is an important part of Java's security strategy.
GC has the following disadvantages such as :-
(1) Garbage-collected heap is that ,contains an overhead that can affect program performance.
(2) Garbage-collected environment have less control over the scheduling of CPU time devoted to freeing objects that are no longer needed.
(3) JVM has to keep track, which objects are being referenced by the executing program, and finalize and free unreferenced objects on the fly.
if you want more details about Garbage Collector with example go to following URL :
GC in Java Tutorial resourceshttp://www.roseindia.net/java/beginners/GarbageCollection.shtml