Types Of References In Java : Strong, Weak , Soft, And Phantom

There are four types of references in Java.

1) Strong References

These type of references we use daily while writing the code. Any object in the memory which has active strong reference is not eligible for garbage collection.

2) 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.

3) Weak References

Weak references are even more awesome. Thats because seemingly the garbage collector has no regard for an object that only has a week reference. What that means is that the garbage collector will make it eligible for garbage collection because object only has a week reference pointing to it. Not only is that awesome and useful, but desirable as well in some scenarios.

4) 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.

Here is an example code which makes use of all kinds of references :

package collections;
 
import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
 
public class References {
	private String status = "Hi I am active";
 
	public String getStatus() {
		return status;
	}
 
	public void setStatus(String status) {
		this.status = status;
	}
 
	@Override
	public String toString() {
		return "ReferenceExample [status=" + status + "]";
	}
 
	public void strongReference() {
		References ex = new References();
		System.out.println(ex);
	}
 
	public void softReference() {
		References ref = getRefrence();
		SoftReference<References> ex = new SoftReference<References>(ref);
		System.out.println("Soft refrence :: " + ex.get());
	}
 
	public void weakReference() {
		int counter = 0;
		WeakReference<References> ex = new WeakReference<References>(
				getRefrence());
		while (ex.get() != null) {
			counter++;
			System.gc();
			System.out.println("Weak reference deleted  after:: " + counter
					+ " attempts - " + ex.get());
		}
	}
 
	public void phantomReference() throws InterruptedException {
		final ReferenceQueue queue = new ReferenceQueue();
		PhantomReference<References> ex = new PhantomReference<References>(
				getRefrence(), queue);
		PhantomReference<References> ex2 = new PhantomReference<References>(
				getRefrence(), queue);
		System.gc();
		queue.remove();
		System.out.println("Phantom reference deleted  - " + ex.get());
	}
 
	private References getRefrence() {
		return new References();
	}
 
	public static void main(String[] args) {
		References ex = new References();
		ex.strongReference();
		ex.softReference();
		ex.weakReference();
		try {
			ex.phantomReference();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

Output of this code would be :

ReferenceExample [status=Hi I am active]
Soft refrence :: ReferenceExample [status=Hi I am active]
Weak reference deleted after:: 1 attempts – null
Phantom reference deleted – null

Uday Ogra

Connect with me at http://facebook.com/tendulkarogra and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *