HomeJavaPhantom References | Java

Phantom References | Java

In Java, memory management is handled automatically, the programmer doesn’t need to worry about reference objects that have been released. There is one downside to this approach, that the programmer cannot know when a particular object will be collected. However, theĀ java.lang.refĀ package defines classes that provide a limited degree of interaction with the garbage collector. The concrete classesĀ SoftReference,Ā WeakReference andĀ PhantomReferenceĀ are subclasses of Reference that interact with the garbage collector in different ways. In our previous journal entry we introduced the Reference Queues, but here we will dig a little deep in discussing the functionality and behavior of Phantom Reference classes. We will also see how it can be used.

Before we start understanding the Phantom Reference classes, let’s first understand the problems with the Finalization.Ā Finalization can be used to perform a cleanup process on objects that garbage collector considers as unreachable.Ā This feature can be utilized to reclaim native resources associated with an object. However,Ā finalizersĀ have many problems associated.

  • We can’t foresee the call of finalize(). This is because of the unpredictability nature of Garbage Collection, therefore the calling of finalize() cannot be predicted.
  • Finalization can slow down an application execution. Managing objects with a finalize() method take more resources from the JVM than normal objects.

There is no guarantee that the object will be garbage collected. The object might never become eligible for GC because it could be reachable through the entire lifetime of the JVM. It is also possible that no garbage collection actually runs from the time the object became eligible and before JVM stops.

You should also use finalization only when it is absolutely necessary. Finalization is a nondeterministic — and sometimes unpredictable — process. The less you rely on it, the smaller the impact it will have on the JVM and your application

Phantom References

phantom reachable, phantomly reachable

An object isĀ phantom reachableĀ if it is neither strongly, softly, or weakly reachable and has been finalized and there is a path from the roots to it that contains at least one phantom reference.

TheĀ PhantomReferenceĀ constructor accepts two arguments:

referentĀ – the object the new phantom reference will refer to
qĀ – the reference is registered with the given queue.

The argumentĀ qĀ represents the instance of theĀ ReferenceQueueĀ class. If the garbage collector determines that the referent of a phantom reference is phantom reachable, then the PhantomReference will be added to thisĀ ReferenceQueue. You can then retrieve the PhantomReference by using theĀ remove()Ā methods of theĀ ReferenceQueueĀ class.

Consider the following example,


ReferenceQueue q = new ReferenceQueue();
PhantomReference pr = new PhantomReference(object, referenceQueue);
// Later on another point
Reference r = q.remove();
// Now, clear up any thing you want

When to use PhantomReference?

Phantom Reference can be used in situations, where sometimes usingĀ finalize()Ā is not the sensible thing to do. This reference type differs from the other types defined inĀ java.lang.refĀ Package because it isn’t meant to be used to access the object, but as a signal that the object has already been finalized, and the garbage collector is ready to reclaim its memory.

Phantom reference objects, which are enqueued after the collector determines that their referents may otherwise be reclaimed. Phantom references are most often used for scheduling pre-mortem cleanup actions in a more flexible way than is possible with the Java finalization mechanism.

Developers tend to use finalize() method to perform the cleanup process of objects which is usually not advisable. As mentioned above, Finalizers have an impact on the performance of the garbage collector since objects with finalizers are slow to garbage collect.

The safest way to know whether an object has been removed from the memory is Phantom References. Let us understand this by taking an example of an application that deals with large data files. We want to load a big file into the memory when there already is another big file in memory which is ready for garbage collection. In such a scenario, we want to wait until the old file is collected before loading a new one. In the scenario like these, phantom references are flexible and safe options to choose. The reference to the old file will be enqueued in the ReferenceQueue object once the old file is finalized. After receiving that reference, we can load the new file into the memory.

In a similar fashion, we can use Phantom References to implement a Connection Pool. We can easily gain control over the number of open connections and can block until one becomes available.

Reference Objects and Garbage Collection

Once there are no strong references to the referent all the Soft References can be garbage collected. All softly reachable objects will be reclaimed before an OutOfMemoryException is thrown.

Weak Reference can be garbage collected when there are no strong or soft references to the referent. However, unlikeĀ Soft Reference, they are garbage collected on a gc even when memory is abundant. They often can be used for ā€œcanonical mappingsā€ where each object has a unique identifier (one-to-one), and in collections of ā€œlistenersā€.

On the other hand,Ā Phantom Reference, can be garbage collected once there are no strong, soft or weak references to the referent. When an object is phantomly reachable, it means the object is already finalized but not yet reclaimed, so the GC enqueues it in aĀ ReferenceQueueĀ for post-finalization processing.

Unlike soft and weak references, phantom references are not automatically cleared by the garbage collector as they are enqueued. An object that is reachable via phantom references will remain so until all such references are cleared or themselves become unreachable.

AĀ PhantomReferenceĀ is not automatically cleared when it is enqueued, so when we remove a PhantomReference from aĀ ReferenceQueue, we must call itsĀ clear()Ā method or allow the PhantomReference object itself to be garbage-collected.

Conclusion

We should try to avoidĀ finalize()Ā as much as possible. There is no guarantee if theĀ finalize()Ā method will be called promptly following garbage collection, or even it will be called. If the finalize method runs for a long time, it can delay execution of finalize() methods of other objects. Instead of relying onĀ finalize(), we can use reference types defined inĀ java.lang.refĀ package.

RELATED ARTICLES

Most Popular