Core Java Tutorials


Introduction

Java Hello World

Java Compilation

JDK New Features

JVM Architecture

Java OOPs Concepts

Java Package

Array

Exception Handling

String

Immutable Class

File Handling

Serialization

MultiThreading

Volatile

Concurrent Package

Collection

Collection Internal

Generics

Cloning

Reflection In Java

Annotation

Class Loader

Java Inner classes

Garbage Collector

JDBC

References In Java

Heap Dump & Thread Dump

References In Java


  As we know that Garbage Collector is responsible for allocating and re-claiming the memory and programmer does not have any control on Garbage Collector. Programmer can just make a request to Garbage Collector not command to it. So with the introduction of java.lang.ref classes, Programmer can have little control over objects when objects will be garbage collected.


 Strong References
In java programming, when you create java references for the objects , by default all the references in java are strong reference. Means when ever these references are not accessible by java active object then these objects will be immediately eligible for Garbage Collection.



 Soft References

The soft reference class is used to create a soft reference to an existing object that is already referred to by a strong reference. The actual object on the heap that is being referred to is called the referent. In order to create a SoftReference to an object, you first need to create a strong reference to that object and then pass that object as a parameter to the constructor of the SoftReference object, as shown in the code below picture.

So, in total, we have 2 strong references created. And the object that represents the soft reference, internally holds a reference to the actual customer object.



 Weak References

Garbage collector has no regard for an object that only has a week reference. garbage collector will make Weak References eligible for garbage collection immediately when strong reference is not refer by any active object in the application .





 Phantom References

Phantom reference is special type of reference available in java and it can be collected at any time by garbage collector. It is not possible to get the object, because reference.get() method always return null. Phantom reference are used in special cases like whenever clean up is required as soon reference is dead. This use case can be acheived easily when phantom reference is used along with reference queue. Phantom reference is the only way to determine whether the object is really dead or not and it is even enqeued to reference queue only if the object is physically removed from memory Example Code: -
Customer customer=new Customer();
PhantomReference phRef = new PhantomReference(customer, referenceQueue);


 Reference Queue

Garbage collector enqueues references to reference queue when it is garbage collected or weakly reachable and these references are used for clean up task. Reference Queue can be used with all types of references, but best combination is with Phantom references. Because phantom reference is only enqueued after it physically removed from memory, where in the case of weak reference, it get enqueued when it is weakly reachable. Reference Queue has few methods to retrieve garbage collected reference for clean up task.

poll - Remove and return reference, if it is available in reference queue else it will return null.
remove - blocks until reference available, if reference is available, then reference is removed and returned.
remove(long timeout) - blocks until timeout expires or reference available.



Code Example

public static void main(String args[]) throws InterruptedException {


phantom-references