The main goal of this project is to give simple example of Phantom References in java.
-
Strong Reference - any object with an active strong reference to it, will never be garbage collected.
-
Soft Reference - the garbage collector may optionally choose to reclaim the memory occupied by the employee object.
SoftReference<X> ex = new SoftReference<>(x);
Remark: Garbage collector will always reclaim the memory of objects that have only soft references pointing to them before it throws an
OutOfMemory Error
. -
Weak Reference - an object that only has a week reference is eligible for garbage collection.
WeakReference<X> ex = new WeakReference<>(x);
Remark: WeakHashMap
Remark: Lapsed listener problem -
Phantom Reference - phantom reference objects are enqueued after the collector determines that their referents may otherwise be reclaimed.
final ReferenceQueue<X> queue = new ReferenceQueue<>(); PhantomReference<X> ex = new PhantomReference<>(x, queue);
Remark: It is used to determine when an object was removed from the memory which helps to schedule memory-sensitive tasks. For example, we can wait for a large object to be removed (reference will appear on the queue) before loading another one.
Remark: Calling aget()
on the Phantom reference always returnsnull
.
Take a look at comments in Main
:
- We create list of large objects.
- We create list of phantom references of large objects (with shared queue).
- Then we make large objects eligible for garbage collecting, by nulling
reference to list:
largeObjects = null;
- Then we notify garbage collection:
System.gc();
- We wait for garbage collector to remove all large objects - we
are reading list of phantom references till all are queued:
reference.isEnqueued()
- At the end we are reading the queue, printing all references and
clearing them:
referenceFromQueue.clear();