Introduction
As we all know that Garbage Collector reclaims memory from objects which are eligible for garbage collection. And this eligibility is decided based upon which kind of references are pointing to that object.
there are four kind of reference in Java :
1. Strong reference
2. Weak Reference
3. Soft Reference
4. Phantom Reference
Garbage collector can collect an object if only weak references are pointing towards it and they are eagerly collected, on the other hand Objects with SoftReference are collected when JVM absolutely needs memory.
Strong reference
in the code, String s = “abc” , reference variable s has strong reference to String object “abc”. Any object which has Strong reference attached to it is not eligible for garbage collection.
Weak reference
Weak Reference are represented using java.lang.ref.WeakReference class and you can create Weak Reference by using following code :
Counter counter = new Counter(); // strong reference created
WeakReference<Counter> weakCounter = new WeakReference<Counter>(counter); //weak reference
counter = null; // now Counter object is eligible for garbage collection
now Counter object is eligible for garbage collection.
one weak reference example
One convenient example of WeakReference is WeakHashMap, which wraps keys as WeakReference which doesn’t prevent them from being Garbage collected.
Soft reference
Soft reference in Java is represented using java.lang.ref.SoftReference class. You can use following code to create a SoftReference in Java
Counter prime = new Counter(); // prime holds a strong reference
SoftReference<Counter> soft = new SoftReference<Counter>(prime) ; //soft reference variable has SoftReference to Counter Object
prime = null;
now Counter object is eligible for garbage collection but only be collected when JVM absolutely needs memory, so it can delay collection compared to weak reference.
comparison between weak and soft reference
SoftReference are more suitable for caches and WeakReference are more suitable for storing meta data.
SoftReference looks perfect for implementing caches. When JVM needs memory it removes object which have only SoftReference pointing towards them.
WeakReference is great for storing meta data e.g. storing ClassLoader reference. If no class is loaded then no point in keeping reference of ClassLoader, a WeakReference makes ClassLoader eligible for Garbage collection as soon as last strong reference removed.
Phantom reference
Phantom reference is represented by java.lang.ref.PhantomReference class.Object which only has Phantom reference pointing them can be collected whenever Garbage Collector likes it.
DigitalCounter digit = new DigitalCounter(); // digit reference variable has strong reference
PhantomReference<DigitalCounter> phantom = new PhantomReference<DigitalCounter>(digit); // phantom reference to object
digit = null;
ReferenceQueue
ReferenceQueue refQueue = new ReferenceQueue(); //reference will be stored in this queue for cleanup
DigitalCounter digit = new DigitalCounter();
PhantomReference<DigitalCounter> phantom = new PhantomReference<DigitalCounter>(digit, refQueue);
Reference of instance will be appended to ReferenceQueue and you can use it to perform any clean-up by polling ReferenceQueue.