An overview of Strong, Weak, Soft and Phantom references in Java.

Prateek Nima
4 min readFeb 20, 2020

When it comes to programming languages particularly considering Java the most discussed topics are OOPs concepts, Multithreading and numerous others to consider. Yet, one of the points which aren’t given a lot of significance are references and garbage collection which turns out to be significant considering the performance of your application.

In this article, we are going to discuss four types of references in Java and how garbage collector behaves differently with each one of them.

1. Strong References

These references are the one which we usually use in our day to day programming.

class Robot
{
//Your code here
}
public class Person
{
public static void main(String[] args)
{
//By default it is a strong reference
Robot rob= new Robot();
//At this point the object is not eligible for GC
rob= null;
//At this point the object is eligible for garbage collection
}
}

2. Weak References

Weak references are the one which points out to Strong reference since we need to specify them explicitly while we write the code. Consider the below code snippet.

class Robot
{
//Your code here
}
public class Person
{
public static void main(String[] args)
{
//By default it is a strong reference
Robot rob= new Robot();
WeakReference<Robot> weakRef = new WeakReference<Robot>(rob);
rob= null;

// At this point the Robot object which was being pointed by
//'rob' object is null but can be still be accessed using
//'weakRef' reference.

//Both 'rob' as well as 'weakRef' are eligible for GC.


Robot rob2 = new Robot();

//Accessing the object using weakRef
rob2 = weakRef.get();
}
}
Robot class object is available for Garbage Collection when we use Weak Reference in Java

As we can see in the above example although the ‘rob’ reference is pointing to null it can be still accessed using the weak reference. When the garbage collection is carried out the object gets cleared out even though it is pointing to the Robot object, thereby improving the application performance.

The weak references have a .get() method which can be used to get the instance of the object to which it is pointing. Weak references are best used to avoid memory leaks. One of the best implementations of weak references is the WeakHashMap.

3. Soft References

Soft references are similar to weak references except that they are only garbage collected when the JVM has an extreme shortage of memory(Unlike weak references which are immediately available for garbage collection).

class Robot
{
//Your code here
}
public class Person
{
public static void main(String[] args)
{
//By default it is a strong reference
Robot rob= new Robot();
SoftReference<Robot> softRef = new SoftReference<Robot>(rob);
rob= null;

// At this point the Robot object which was being pointed by
//'rob' object is null but can be still be accessed using
//'softRef' reference.

//Both 'rob' as well as 'softRef' are eligible for GC but it
//will only collected in the scenerio when JVM is in need of
//memory.


Robot rob2 = new Robot();

//Accessing the object using softRef
rob2 = softRef.get();
}
}
Robot class object is available for Garbage Collection(In this case when the memory is full) when we use Soft Reference in Java

4. Phantom References

Phantom references are the least used references in Java. These references are only garbage collected when none of the references namely strong references, weak references or soft references is being pointed to the object belonging to phantom references. They are always used along with reference queue and to keep a track of objects which have been finalized.

class Robot
{
//Your code here
}
public class Person
{
public static void main(String[] args)
{
//By default it is a strong reference
Robot rob= new Robot();
//Creating new reference queue
ReferenceQueue<Robot> queueRef = new ReferenceQueue<Robot>();
//Creating new phantom reference of type robot
PhantomReference<Robot> phantomRef = new
PhantomReference<Robot>(rob,queueRef);

rob = null;
Robot rob2 = new Robot();

//Accessing the object using phantomRef gives null
rob2 = softRef.get();
}
}

Basically, phantom references are used when we want to load another large object only after the previous object has been garbage collected thereby avoiding out of memory error.

In the above article, we discussed all the four types of references and understanding the way the manner in which garbage collector treats every single one of them.

If you liked the article and helped you to learn something new then please hit the clap button present below and share with your friends. Hope you have a great day ahead! 😉

--

--