Sunday, 26 February 2017

Reference - Type of Reference in Java

In java memory automatically managed by garbage collector there is no need of explicit declaration of memory management. But with the introduction of java.lang.ref classes we have a little control over when the object will be garbage collected.

Type of Reference –

1.      Strong References
2.      Soft References
3.      Weak References
4.      Phantom References

Strong References
Any object in the memory which has active strong reference is not eligible for garbage collection. For example, in the below program, reference variable ‘a’ is a strong reference which is pointing to class A-type object. At this point of time, this object can’t be garbage collected as it has strong reference.

Code Snippet –
/**
 * @author Manoj
 */
public class Test {
  public static void main(String args[]) {
     Student std = new Student(); //strong reference
     std = null; // now object is available for GC
  }
}
class Student {
       //student class
}

Weak References
VM ignores the weak references. That means objects which has only week references are eligible for garbage collection. They are likely to be garbage collected when JVM runs garbage collector thread. JVM doesn’t show any regard for weak references.

Code snippet-
import java.lang.ref.WeakReference;

/**
 * @author Manoj
 */
public class Test {
  public static void main(String args[]) {
       Student std = new Student(); //strong reference
       //Creating Weak Reference to Student-type object to which 'std' is also pointing.
    WeakReference<Student> weakStudent = new WeakReference<Student>(std);
    std = null;    //Now, Student-type object to which 'std' is pointing earlier is available for garbage collection.
    std = weakStudent.get(); //we can retrieve back the object which has been weakly referenced.
   }

 }
class Student {
   //student class
}

Soft References
The objects which are softly referenced will not be garbage collected (even though they are available for garbage collection) until JVM badly needs memory. These objects will be cleared from the memory only if JVM runs out of memory. You can create a soft reference to an existing object by using  java.lang.ref.SoftReference class. Below is the code example on how to create a soft reference.

Code snippet –
import java.lang.ref.SoftReference;

/**
 * @author Manoj
 */
public class Test {
  public static void main(String args[]) {
     Student std = new Student(); //strong reference
     //Creating Soft Reference to Student-type object to which 'std' is also pointing
     SoftReference<Student> softStudent = new SoftReference<Student>(std);
     std = null;    //Now, Student-type object to which 'std' is pointing earlier is eligible for garbage collection. But, it will be garbage collected only when JVM needs memory.
    std = softStudent.get();    //You can retrieve back the object which has been softly referenced
    }
}

class Student {
       //student class
}

Phantom References
The objects which are being referenced by phantom references are eligible for garbage collection. But, before removing them from the memory, JVM puts them in a queue called ‘reference queue’. They are put in a reference queue after calling finalize () method on them. You can’t retrieve back the objects which are being phantom referenced. That means calling get () method on phantom reference always returns null.

Code snippet-
import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;

/**
 * @author Manoj
 */
public class Test {
  public static void main(String args[]) {
       Student std = new Student(); // strong reference
       // Creating ReferenceQueue
       ReferenceQueue<Student> refQueue = new ReferenceQueue<Student>();
       // Creating Phantom Reference to Student-type object to which 'std' is also
       // pointing
       PhantomReference<Student> phantomStudent = new PhantomReference<Student>(std, refQueue);
       std = null; // Now, Student-type object to which 'std' is pointing earlier is
              // available for garbage collection. But, this object is
              // kept in 'refQueue' before removing it from the memory.
       std = phantomStudent.get(); // it always returns null
  }
}

class Student {
  // student class

}

1 comment: