Skip to main content

What are soft, weak, and phantom references?

Mid Java
Quick Answer Reference types control GC behavior. Strong reference (normal): GC never collects. Soft reference (SoftReference): GC collects only when memory is low - good for caches. Weak reference (WeakReference): GC collects at next GC cycle - useful for canonicalization maps. Phantom reference (PhantomReference): enqueued after object is collected - for cleanup after GC. WeakHashMap uses weak keys.

Answer

SoftReference cleared when memory is low; used for caching.
WeakReference cleared during GC if no strong references exist.
PhantomReference used for cleanup before object collection.
S
SugharaIQ Editorial Team Verified Answer

This answer has been peer-reviewed by industry experts holding senior engineering roles to ensure technical accuracy and relevance for modern interview standards.

Want to bookmark, take notes, or join discussions?

Sign in to access all features and personalize your learning experience.

Sign In Create Account

Source: SugharaIQ

Ready to level up? Start Practice