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.

Object o = new Object();

Soft

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

SoftReference soft = new SoftReference(bean);

Weak

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

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.

PhantomReference soft = new PhantomReference(bean);


Published

04 April 2016

Category

development

Tags