Skip to content

mtumilowicz/phantom-reference

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

phantom-reference

The main goal of this project is to give simple example of Phantom References in java.

reference types

  1. Strong Reference - any object with an active strong reference to it, will never be garbage collected.

  2. 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.

  3. 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

  4. 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 a get() on the Phantom reference always returns null.

description

Take a look at comments in Main:

  1. We create list of large objects.
  2. We create list of phantom references of large objects (with shared queue).
  3. Then we make large objects eligible for garbage collecting, by nulling reference to list:
    largeObjects = null;
    
  4. Then we notify garbage collection:
    System.gc();
    
  5. We wait for garbage collector to remove all large objects - we are reading list of phantom references till all are queued:
    reference.isEnqueued()
    
  6. At the end we are reading the queue, printing all references and clearing them:
    referenceFromQueue.clear();
    

Releases

No releases published

Packages

No packages published

Languages