encyclopegia.org

Phantom reference

A phantom reference is a kind of reference in Java, where the memory can be reclaimed. The class is java.lang.ref.PhantomReference . The 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. 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. In Java 8 and earlier versions, the reference needs to be cleared before the memory for a finalized referent can be reclaimed. A change in Java 9 will allow memory from a finalized referent to be reclaimable immediately.

A phantom reference is a kind of reference in Java, where the memory can be reclaimed. The class is java.lang.ref.PhantomReference . The 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. 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. In Java 8 and earlier versions, the reference needs to be cleared before the memory for a finalized referent can be reclaimed. A change in Java 9 will allow memory from a finalized referent to be reclaimable immediately. ==Use== Phantom references are of limited use, primarily narrow technical uses. First, it can be used instead of a finalize method, guaranteeing that the object is not resurrected during finalization. This allows the object to 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 java.lang.ref.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. ==Example== import java.lang.ref.PhantomReference; import java.lang.ref.ReferenceQueue; class Resource { private final String name; public Resource(String name) { this.name = name; } public String getName() { return name; } } public class PhantomReferenceExample { public static void main(String[] args) throws InterruptedException { Resource resource = new Resource("My resource"); ReferenceQueue queue = new ReferenceQueue<>(); PhantomReference phantomRef = new PhantomReference<>(resource, queue); resource = null; System.gc(); Reference ref = queue.poll(); if (ref != null) { System.out.printf("Object is ready to be collected: %s%n", ((PhantomReference )ref).get()); } } } ==References==

References

  1. java.lang.ref (Java Platform SE 8 ) · retrieved 2026-11-17 · rev 1334339403
    Cite this reference

    MLA "java.lang.ref (Java Platform SE 8 )." "java. 2026-11-17. https://docs.oracle.com/javase/8/docs/api/java/lang/ref/package-summary.html. Accessed 24 November 2026.

    Chicago "java.lang.ref (Java Platform SE 8 )." "java. 2026-11-17. https://docs.oracle.com/javase/8/docs/api/java/lang/ref/package-summary.html.

    APA "java. (2026). java.lang.ref (Java Platform SE 8 ). Retrieved November 24, 2026, from https://docs.oracle.com/javase/8/docs/api/java/lang/ref/package-summary.html

  2. hg: jdk9/hs-rt/jdk: 8071507: (ref) Clear phantom reference as soft and weak references do · retrieved 2026-11-17 · rev 1334339403
    Cite this reference

    MLA "hg: jdk9/hs-rt/jdk: 8071507: (ref) Clear phantom reference as soft and weak references do." oracle. 2026-11-17. https://mail.openjdk.java.net/pipermail/jdk9-hs-rt-changes/2015-December/001914.html. Accessed 24 November 2026.

    Chicago "hg: jdk9/hs-rt/jdk: 8071507: (ref) Clear phantom reference as soft and weak references do." oracle. 2026-11-17. https://mail.openjdk.java.net/pipermail/jdk9-hs-rt-changes/2015-December/001914.html.

    APA oracle. (2026). hg: jdk9/hs-rt/jdk: 8071507: (ref) Clear phantom reference as soft and weak references do. Retrieved November 24, 2026, from https://mail.openjdk.java.net/pipermail/jdk9-hs-rt-changes/2015-December/001914.html

  3. Understanding Weak References · retrieved 2026-11-17 · rev 1334339403
    Cite this reference

    MLA "Understanding Weak References." Nicholas, Ethan (May 4, 2006). 2026-11-17. https://web.archive.org/web/20100819115659/http://weblogs.java.net/blog/2006/05/04/understanding-weak-references. Accessed 24 November 2026.

    Chicago "Understanding Weak References." Nicholas, Ethan (May 4, 2006). 2026-11-17. https://web.archive.org/web/20100819115659/http://weblogs.java.net/blog/2006/05/04/understanding-weak-references.

    APA Nicholas, Ethan (May 4, 2006). (2026). Understanding Weak References. Retrieved November 24, 2026, from https://web.archive.org/web/20100819115659/http://weblogs.java.net/blog/2006/05/04/understanding-weak-references

Cite this article

MLA

"Phantom reference." encyclopegia.org, 2026, https://encyclopegia.org/wiki/phantom-reference. Accessed 24 November 2026.

Chicago

"Phantom reference." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/phantom-reference.

APA

encyclopegia.org. (2026). Phantom reference. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/phantom-reference

Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON

Related articles

Siemens SL10

enwiki · references available

Soft reference

enwiki · references available
CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1334339403 · 0.5 · snapshot enwiki-001334339403

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.