Phantom reference

A phantom reference is one of the strengths or levels of 'non strong' reference defined in the Java programming language; the others being weak and soft.[1] Phantom reference are the weakest level of reference in Java; in order from strongest to weakest, they are: strong, soft, weak, phantom.

An object is phantomly referenced after it has been finalized, but before its allocated memory has been reclaimed.

Use

Phantom references are of limited use, primarily narrow technical uses.[2] One use is during a finalize method, which guarantees that the object is not resurrected during finalization and thus can be garbage collected in a single cycle, rather than needing to wait for a second GC cycle to ensure that it has not been resurrected. A second use is to detect exactly when an object has been removed from memory (by using in combination with a ReferenceQueue object), ensuring that its memory is available, for example deferring allocation of a large amount of memory (e.g., a large image) until previous memory is freed.

See also

References

  1. "Package java.lang.ref". Java™ Platform, Standard Edition 7 API Specification. Oracle. Retrieved 12 December 2011.
  2. Nicholas, Ethan (May 4, 2006). "Understanding Weak References". java.net. Retrieved October 1, 2010.