Every instance object you create with ‘new’ operator or by class.forName is called strong type of reference. But Java provides other type of reference to help GC process and manage memory efficiently. Here we are discussing other reference types –
An object is softly reachable if it is not strongly reachable and there is a path to it with no weak or phantom references, but one or more soft references. The garbage collector might or might not reclaim a softly reachable object depending on how recently the object was created or accessed, but is required to clear all soft references before throwing an OutOfMemoryError.
If heap memory is running low, the garbage collector may, at its own discretion, find softly reachable objects that have not been accessed in the longest time and clear them (set their reference field to null).
Simplest way to create a soft reference is like this –
Object obj = new Object();
SoftReference softRef = new SoftReference(obj);
obj = null;
You can get real object from soft reference by following code –
obj = softRef .get();
if (obj == null) // always check this, GC might have freed that
softRef = new SoftReference(obj = new Object()); // create new one
Soft reference are useful to hold cache object pools which can be recreated and allow GC to free those if memory is low.
An object is weakly reachable when the garbage collector finds no strong or soft references, but at least one path to the object with a weak reference. Weakly reachable objects are finalized some time after their weak references have been cleared. The only real difference between a soft reference and a weak reference is that the garbage collector uses algorithms to decide whether or not to reclaim a softly reachable object, but always reclaims a weakly reachable object.
Weak reference can be created same way like soft references, also can be retrieved in similar way. Class which is used to hold weak references is WeakReference.
Weak reference are useful when you want to have reference till some some period of time of application like a thread execution time.
Though it is another type of reference it has nothing to do with operation on object. This is used in scenario where you want some cleanup before object been garbage collected. It can be used to perform pre-mortem cleanup operations. A PhantomReference must be used with the ReferenceQueue class. The ReferenceQueue is required because it serves as the mechanism of notification. When the garbage collector determines an object is phantom reachable, the PhantomReference object is placed on its ReferenceQueue. The placing of the PhantomReference object on the ReferenceQueue is your notification that the object the PhantomReference object referred to has been finalized and is ready to be collected. This allows you to take action just prior to the object memory being reclaimed.
Below we are discussing steps which GC takes when these reference need to be collected.
Garbage collector and reference interaction
Each time the garbage collector runs, it optionally frees object memory that is no longer strongly reachable.
If the garbage collector discovers an object that is softly reachable, the following occurs:
- The SoftReference object’s referent field is set to null, thereby making it not refer to the heap object any longer.
- The heap object that had been referenced by the SoftReference is declared finalizable.
- When the heap object’s finalize() method is run and its memory freed, the SoftReference object is added to its ReferenceQueue, if it exists.
If the garbage collector discovers an object that is weakly reachable, the following occurs:
- The WeakReference object’s referent field is set to null, thereby making it not refer to the heap object any longer.
- The heap object that had been referenced by the WeakReference is declared finalizable.
- When the heap object’s finalize() method is run and its memory freed, the WeakReference object is added to its ReferenceQueue, if it exists.
If the garbage collector discovers an object that is phantomly reachable, the following occurs:
- The heap object that is referenced by the PhantomReference is declared finalizable.
- Unlike soft and weak references, the PhantomReference is added to its ReferenceQueue before the heap object is freed. (Remember, all PhantomReference objects must be created with an associated ReferenceQueue.) This allows for action to be taken before the heap object is reclaimed.
The idea behind these classes is to avoid pinning an object in memory for the duration of the application. Instead, you softly, weakly, or phantomly refer to an object, allowing the garbage collector to optionally free it. This usage can be beneficial when you want to minimize the amount of heap memory an application uses over its lifetime. You must remember that to make use of these classes you cannot maintain a strong reference to the object. If you do, you waste any benefit these classes offer.