Reference Types
Strong, Weak, Soft, Phantom references
6 min read
Reference Types
Java provides different reference strengths to control GC behavior.
- Strong: Default reference
- Soft: Memory-sensitive caches
- Weak: GC when weakly reachable
- Phantom: Cleanup hooks
Code Examples
Weak reference usage
java
1
2WeakReference<User> ref = new WeakReference<>(new User());
3 Use Cases
- Caching
- Memory-sensitive applications
Common Mistakes to Avoid
- Using weak references incorrectly