Tuesday, 9 April 2013

Java Garbage Collection


         GARBAGE COLLECTION IN JAVA

Garbage collection is the process of automatically freeing objects that are no longer referenced by the program.
The Object created in JAVA is stored in the Heap Area of the memory.


•    The name "garbage collection" implies that objects no longer needed by the program are "garbage" and can be thrown away.
•       A more accurate and up-to-date metaphor might be "memory recycling." When an object is no longer referenced by the program, the heap space it occupies can be recycled so that the space is made available for subsequent new objects.




Important Point about Garbage Collection In Java:

 1. Objects are created on Heap in Java, while it’s class variable or static members are created in method area of Java memory space and heap and method area is shared between different thread..
 2.  Garbage collection is a mechanism provided by JVM to reclaim heap space.
 3.  Garbage Collection in Java is carried by a daemon thread called Garbage Collector.
 4.  Before removing a n object from memory Garbage collection thread invokes finalize() method of that object.

5.    As a Java Programmer No one can force Garbage collection in Java       to perform operation. If JVM thinks it needs a garbage collection    based on Java heap sized.

6.    There are methods like System.gc() and Runtime.gc() which is used to send request of Garbage Collection  to JVM but it’s not guaranteed that garbage collection will happen.

7.    If there is no memory space for crating new object in HEAP Java Virtual Machine throws OutOfMemoryError  or java.lang.OutOfMemoryError Heap space.





Generally  An Object become eligible for Garbage Collection in Java On following cases:

1)  An Object becomes eligible for Garbage Collection or GC if it’s not reachable from any live thread or any static references . In other words an Object becomes eligible for Garbage collection if its all references are null.
2)   Cyclic dependencies are not counted as reference. 
 
       If Object A has reference of object B and vice versa and they don’t have any other live reference then both Objects A and B will be eligible for Garbage Collection.

3)   All reference of that object explicitly set to null
       e.g. object= NULL

4)   Object is created inside a block and reference goes out of scope      when control exit that block.

5).  Parent Object set to NULL, if an object holds reference of   another object and when you set container object’s reference   NULL,  child or contained object automatically becomes eligible   for garbage collection.




Heap Generations For Garbage Collection in Java


•Java Objects are created in Heap. 
•Heap is divided into three parts:
  i). Young generation(New Generation)
    a) Eden space,
    b)Survivor1
    c)Survivor2 space.
  ii).Tenured or Old Generation
  iii).Perm Area of Heap(Permanent Heap). 


•    When and Object First Created in Heap it’s gets created in New Generation inside Eden Space.

•    If Object survives its gets moved to survivor 1 and then Survivor 2 with the help of Minor garbage collection.

•      Finally it moves to Old or Tenured generation with the help of Major garbage Collection.

•    Permanent Generation of Heap or Perm Area of Heap is somewhat special and it is used to store Meta data related to classes and method in JVM, it also hosts String pool provided by JVM.

•    Some times the Garbage Collection is happened in Perm Area of Heap but it’s depends on the implementation of JVM.



Types Of Garbage Collector in JAVA

1.Concurrent Garbage  Collector
2.  Full Garbage Collector
  Concurrent Garbage Collector
Ø  Concurrent garbage collector in Java uses a single    garbage collector thread that runs concurrently with the  application threads with the goal of completing the collection of the tenured old generation before it becomes full.

Ø  In normal, operation, the concurrent garbage collector is able to do most of its work with the application threads still running.

Full Garbage Collector

Ø       If the Concurrent Garbage collector is unable to finish before the tenured generation(old Generation) fill up, the application is paused and the collection is completed with all the application threads stopped. Such collections with the applications stopped are referred as Full Garbage Collections or Full GC.
         
           JVM always try to avoid or minimize Full Garbage Collection or Full GC because it affects performance of Java application.

No comments:

Post a Comment