Thursday, December 27, 2012

Republic of Garbage Collections

Republic of Garbage Collections
...and rules of life of GC Citizens:




package com.jeeprojectsnippets.gc.eligible;

public class Eligible {
      
       int j;

       public static void main(String[] args) {
             Eligible first = new Eligible(11);      // 11
             Eligible second = new Eligible(12);     // 12
             Eligible third = new Eligible(13);      // 13
             Eligible fourth = new Eligible(14);     // 14
             fourth = first.run(third); // fourth is set to null and marked for GC
             second = null; // second and fourth have been set to null and marked for GC

             tryTest("First: ", first);
             tryTest("Second: ", second);
             tryTest("Third: ", third);
             tryTest("Fourth: ", fourth);
            
             System.gc();
            
             tryTest("First: ", first);
             tryTest("Second: ", second);
             tryTest("Third: ", third);
             tryTest("Fourth: ", fourth);
       }

       public Eligible(int j) {
             this.j = j;
       }

       private Eligible run(Eligible eligible) {
             eligible = null;    // third is not set to null - this copy of 
                                 // eligible reference exists!
             return eligible;    // null is returned
       }

       public String toString() {
             return "" + j;
       }

       public static void tryTest(String msg, Eligible eligible) {
             try {
                    System.out.print(msg);
                    System.out.println(eligible);
             } catch (Exception e) {
                    System.out.print(msg);
                    System.out.println((String) null);
             }
       }
}
And the output of the test is:
First: 11
Second: null
Third: 13
Fourth: null
First: 11
Second: null
Third: 13
Fourth: null


An object becomes eligible for garbage collection in Java in following situations:
a. If an object has only live references via WeakHashMap
b. Parent object is set to null
c. All references of that object explicitly are set to null e.g. obj = null
d. Object is created inside a block/loop and reference is out of scope.

Java objects are created in Heap, and we can say about Heap Generations.
Heap is divided into 3 generations for sake of garbage collection in Java:
1. Young generation,
2. Tenured or Old Generation,
3. and Perm Area of Heap.

New objects are created into young generation and subsequently moved to old generation.

Young/New Generation is further divided into 3 parts known as:
1.1.  Eden space,
1.2.  Survivor 1 space
1.3.  and Survivor 2 space.

Perm Area of Heap (Permanent generation) 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. That can explain why String is immutable in Java.

We can use methods like System.gc () and Runtime.gc () which is used to send request of Garbage collection to JVM, but it is not guaranteed that garbage collection will happen at all.

The garbage collection thread invokes finalize() method before removing an object from memory of the object and gives us an opportunity to perform any sort of cleanup required.

Garbage collection tuning generally depends on fact what kind of object application has and what are there average lifetime etc.

Read more:

No comments: