Fundamentals 6 min read

Understanding Java’s Four Reference Types: Strong, Soft, Weak, and Phantom

This article explains how Java’s garbage collector interacts with the four reference types—Strong, Soft, Weak, and Phantom—detailing their behavior, typical use cases, and code examples that show when each reference allows an object to be reclaimed.

Programmer1970
Programmer1970
Programmer1970
Understanding Java’s Four Reference Types: Strong, Soft, Weak, and Phantom

Java's garbage collector automatically reclaims memory of objects that are no longer reachable. To cooperate with the collector, Java defines four reference types: Strong, Soft, Weak, and Phantom.

1. Strong Reference

Strong references are the default. As long as a strong reference exists, the object is considered reachable and will not be collected, even under memory pressure. Object strongReference = new Object(); As long as strongReference is not set to null or reclaimed, the referenced Object remains alive.

2. Soft Reference

Soft references are intended for memory‑sensitive caches. The GC will enqueue a soft‑referenced object for reclamation only when the JVM is about to throw an OutOfMemoryError. The object can still be accessed via softReference.get() until it is collected.

SoftReference<Object> softReference = new SoftReference<>(new Object());

When memory is low, the GC may reclaim the object, but the reference remains until then.

3. Weak Reference

Weak references are weaker than soft references. An object reachable only through a weak reference is reclaimed at the next GC cycle, regardless of memory availability.

WeakReference<Object> weakReference = new WeakReference<>(new Object());

After a GC run, the Object may be collected even if the system has sufficient memory.

4. Phantom Reference

Phantom references are the weakest; they do not prevent reclamation and cannot be used to retrieve the referent. Their sole purpose is to receive a notification via a ReferenceQueue after the object is collected.

ReferenceQueue<Object> queue = new ReferenceQueue<>();
PhantomReference<Object> phantomReference = new PhantomReference<>(new Object(), queue);

When the GC reclaims the object, the phantom reference is enqueued, allowing the program to perform cleanup actions.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaGarbage CollectionPhantom ReferenceWeak ReferenceReference TypesSoft ReferenceStrong Reference
Programmer1970
Written by

Programmer1970

Formerly called 'Code to 35'. Add our main WeChat ID to access a wealth of shared resources (algorithms, interview prep, tech stacks: Java, Python, Go, big data). We mainly share serious development techniques, focusing on output-driven input. Occasionally we post life snippets and gossip. Our aim is to attract precise traffic and test advertising opportunities.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.