Java Reference Types

To reimburse different needs for references, Java provisioned different level of references type since 1.2.
It actually meets some application requirement like caches.

Strong

The normal reference, unable to GC until no reference exists.

1
Object o = new Object();

Soft

When memory about to overflow, good for saving memory when space sensitive.

1
SoftReference soft = new SoftReference(bean);

Weak

Will be collected at next GC cycle. Designed for objects that have short life.

1
WeakReference soft = new WeakReference(bean);

Phantom

weakest reference. As if there is no reference.
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.

1
PhantomReference soft = new PhantomReference(bean);

Java Reference Types
https://rug.al/2016/2016-04-04-java-reference-types/
Author
Rugal Bernstein
Posted on
April 4, 2016
Licensed under