WeakReference vs SoftReference vs PhantomReference
Fast recall
Quick revision
Core answer
Whether you know when and why to use each type — misusing them causes memory leaks or premature collection.
Using WeakReference for a cache (objects get collected too aggressively) or SoftReference everywhere (can cause GC pauses trying to free memory).
Mention ReferenceQueue, that PhantomReference.get() always returns null (unlike the others), and WeakHashMap as a practical application.
Interview-ready guide
Interview answer
Java has four reference strengths — the mistake is using the wrong one and getting either premature collection or a memory leak.
When each is cleared:
- Strong — never while a strong reference exists (the default)
- Soft — only when the JVM needs memory (before OOM) — right for memory-sensitive caches
- Weak — on the next GC cycle regardless of memory — right for canonicalization maps, wrong for caches
- Phantom — after finalization;
get()always returns null; used only for post-collection cleanup notification
Practical applications:
WeakHashMap— entries auto-remove when keys lose strong references; GC collects on next cycle regardless of available memoryCleaner(Java 9+) —Cleaner.register(obj, cleanupRunnable)runs cleanup when the object becomes unreachable; replaces the deprecatedfinalize()
Interview differentiator:
PhantomReference.get()always returnsnull— unlike soft and weak; you can never access the referent through a phantom reference- Using
WeakReferencefor caching gives near-zero hit rates — objects are collected on GC regardless of available heap
I recommend SoftReference for memory-sensitive caching and WeakHashMap for metadata that should automatically disappear when the subject is no longer in use.
WeakReference vs SoftReference vs PhantomReference
Direct Answer
Java provides three reference types weaker than strong references. SoftReference objects are kept alive until the JVM needs memory (good for caches). WeakReference objects are collected in the next GC cycle once no strong references remain (good for canonicalization maps). PhantomReference objects are enqueued after the object is finalized but before memory is reclaimed, letting you perform cleanup without relying on finalizers.
Common Mistakes & Pitfalls
Using WeakReference for a cache (objects get collected too aggressively) or SoftReference everywhere (can cause GC pauses trying to free memory).
Check your understanding
Answer these without looking back at the article.
- How does WeakHashMap use WeakReferences?
- What replaced finalizers in Java 9?
- How would you implement a memory-sensitive LRU cache in Java?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.