Quick revision
The short answer
A strong reference is a normal reference, and a strongly reachable object is never collected. A SoftReference is cleared at the collector's discretion under memory pressure, and always before an OutOfMemoryError, which made it a tool for memory-sensitive caches, though bounded caches are usually better; a WeakReference is cleared at the next GC once its object is only weakly reachable, which is how WeakHashMap attaches metadata to objects owned elsewhere. A PhantomReference's get() always returns null; it is enqueued after its object becomes unreachable so we can run cleanup, which is how java.lang.ref.Cleaner works, and reference queues tell code when references have been cleared.
What to remember
In this answer, we saw that strong references keep objects alive as long as they are reachable, soft references are cleared under memory pressure and always before an OutOfMemoryError, weak references are cleared at the next GC once their object is only weakly reachable, and phantom references never return their object but are enqueued after it becomes unreachable. Soft references suit memory-sensitive caches in theory, though bounded caches work better; weak references power WeakHashMap; phantom references and reference queues power Cleaner, which, with explicit close(), replaces finalize.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A strong reference is a normal reference, and a strongly reachable object is never collected. A SoftReference is cleared at the collector's discretion under memory pressure, and always before an OutOfMemoryError, which made it a tool for memory-sensitive caches, though bounded caches are usually better; a WeakReference is cleared at the next GC once its object is only weakly reachable, which is how WeakHashMap attaches metadata to objects owned elsewhere. A PhantomReference's get() always returns null; it is enqueued after its object becomes unreachable so we can run cleanup, which is how java.lang.ref.Cleaner works, and reference queues tell code when references have been cleared.
Why this question is asked
Most Java code only ever uses strong references, and that is how it should be. But the four reference types explain several things we meet in real work: why a WeakHashMap entry disappears, why a soft-reference cache makes GC slow, and how the JDK frees native memory behind a direct buffer. Interviewers ask this to see whether we understand reachability, the idea the garbage collector is built on. In this answer, we will recap reachability, then go through each reference type with code and a practical use, and finish with reference queues and Cleaner.
1A Quick Recap of Reachability
The garbage collector frees objects that the program can no longer reach. It starts from roots, such as local variables on thread stacks and static fields, and follows references. Any object it can reach is live and stays.
The java.lang.ref package adds weaker kinds of references. A reference object, such as a WeakReference, holds a pointer to another object, its referent, but that pointer does not keep the referent alive the way a normal reference does. So an object can be strongly reachable, reachable through a chain of normal references; softly reachable, reachable only through soft references; weakly reachable; or phantom reachable. The collector treats each level differently.
| Type | Cleared when | Practical use |
|---|---|---|
| Strong | never, while reachable | all normal code |
SoftReference | under memory pressure, always before OOM | memory-sensitive caches (prefer bounded caches) |
WeakReference | at the next GC once only weakly reachable | WeakHashMap keys, listeners, canonical maps |
PhantomReference | get() is always null; enqueued after unreachable | cleanup of native resources, Cleaner |
2Strong References
A strong reference is an ordinary variable or field. As long as an object is reachable through strong references from a root, the collector will never free it, no matter how short memory is; if memory runs out, the JVM throws OutOfMemoryError instead.
This is also why memory leaks happen in Java: a forgotten strong reference, for example in a static map, keeps objects alive forever.
3Soft References
A SoftReference lets its referent stay in memory as long as there is room, and lets the collector clear it when memory gets tight. The JVM guarantees that all softly reachable objects are cleared before it throws OutOfMemoryError. Between those two points, the timing is up to the collector; HotSpot uses a policy based on free heap and how recently each reference was used.
The classic use was a memory-sensitive cache: keep computed images or reports as long as memory allows. In practice it is usually a poor choice, because the cache grows until the heap is nearly full and then GC decides what to evict, which makes heap usage and GC cost unpredictable. A cache with an explicit size limit, such as Caffeine with maximumSize, is better in almost every case.
4Weak References
A WeakReference does not keep its referent alive at all. As soon as the object is reachable only through weak references, the next GC that processes it clears the reference.
The main use is attaching information to objects that someone else owns, without keeping them alive. WeakHashMap does exactly that: its keys are held through weak references, so when the rest of the program drops a key object, the entry is removed on a later map operation. Listener registries and canonicalising maps use weak references for the same reason. Notice the comment on System.gc(): it is only a request, so tests that rely on it are fragile.
5Phantom References, Reference Queues and Cleaner
A PhantomReference is the weakest kind. Its get() always returns null, so we can never reach the object through it. Its only purpose is notification: once the object has become unreachable, the collector puts the PhantomReference on a ReferenceQueue we supplied, and our code can then release resources associated with the object, such as native memory or a file handle. A reference queue works for soft and weak references too: when the collector clears a reference registered with a queue, it enqueues it, so code can find out which references were cleared and remove stale entries.
java.lang.ref.Cleaner, added in Java 9, packages this pattern. We register an object and a cleanup action; Cleaner holds a phantom reference to the object and runs the action on its own thread after the object becomes unreachable, or earlier if we call clean() ourselves.
Notice the comment in the constructor. If the cleanup action captured this, for example as a lambda calling a method of the buffer, the action itself would keep the buffer strongly reachable and it would never be cleaned. The JDK's direct ByteBuffers use the same mechanism internally to free their native memory.
6Follow-up questions
Why is Cleaner preferred to finalize?
A finalize() method runs on the object itself, so it can resurrect the object by storing this somewhere, it delays reclamation by at least one extra GC cycle, it runs on a shared finalizer thread in no guaranteed order or time, and it may never run at all. Cleaner's action is a separate object that cannot reach the cleaned object, so resurrection is impossible; it can be run explicitly and exactly once through clean(); and each library can use its own Cleaner thread. Finalization is deprecated for removal since JDK 18 (JEP 421), so Cleaner, together with explicit close(), is the supported way.
Can a weakly reachable object be collected while I am using it?
Not while we hold a strong reference to it. The usual pattern is to call get() once, store the result in a local variable, check it for null and use the local. Calling get() twice can return the object the first time and null the second, if a GC ran in between.
Are phantom references cleared automatically?
Since Java 9, yes: phantom references are cleared by the collector when they are enqueued, like soft and weak references. Before Java 9, the referent stayed in memory until the program cleared the phantom reference itself, which was an easy source of leaks.
7Conclusion
In this answer, we saw that strong references keep objects alive as long as they are reachable, soft references are cleared under memory pressure and always before an OutOfMemoryError, weak references are cleared at the next GC once their object is only weakly reachable, and phantom references never return their object but are enqueued after it becomes unreachable. Soft references suit memory-sensitive caches in theory, though bounded caches work better; weak references power WeakHashMap; phantom references and reference queues power Cleaner, which, with explicit close(), replaces finalize.
Open with
Java has four reference strengths. A strong reference is a normal variable or field; as long as an object is strongly reachable from a root, the collector never frees it.
Then, if they want more
A SoftReference lets the collector clear its referent when it needs memory, and it guarantees that all softly reachable objects are cleared before throwing OutOfMemoryError. It was meant for memory-sensitive caches, but in practice a bounded cache like Caffeine with maximumSize is better, because soft caches fill the heap and make GC unpredictable.
A WeakReference does not keep its object alive at all; once the object is only weakly reachable, the next GC clears it. The typical use is WeakHashMap, which holds its keys weakly, so I can attach metadata to objects owned elsewhere and the entry goes away when the owner drops the object.
A PhantomReference's get always returns null; it exists only so that after the object becomes unreachable, the reference is enqueued on a ReferenceQueue and I can release associated resources. That is how java.lang.ref.Cleaner works, and it is what I use as a safety net for native resources, with explicit close and try-with-resources as the main path.
Reference queues work for all three kinds and let code find out which references were cleared.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 9, Java 18
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- HotSpot Garbage Collection Tuning Guide, JDK 21
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain strong, soft, weak and phantom references in Java with one practical use for each.": https://scrivio-interview-library.netlify.app/java/garbage-collection/e8f85b14be/