Weak Reference and Soft Reference are special types of references in Java that allow the Garbage Collector (GC) to reclaim objects under certain conditions. They are part of the java.lang.ref package and are mainly used for memory-sensitive applications such as caches, memory management utilities, and resource optimization.
Key Points: • Weak references are cleared as soon as an object has no strong references. • Soft references are retained longer and are cleared only when the JVM needs memory. • Both help reduce memory consumption and prevent memory leaks. • Soft references are commonly used for caching. • Weak references are commonly used for lookup tables and metadata storage.
Why Do We Need Special References?
Normally, Java uses Strong References.
Example:
Employee employee =
new Employee();As long as employee exists, the object cannot be garbage collected.
Sometimes we want:
• Objects to remain available if memory permits. • Objects to be removed automatically when no longer needed.
For such cases, Java provides:
• SoftReference • WeakReference
Reference Types in Java
Strong Reference
Strong Reference | | Object
• Default reference type. • Object is never garbage collected while a strong reference exists.
Soft Reference
Soft Reference | | Object
• Object remains in memory as long as sufficient memory is available. • Garbage Collector removes it only when memory becomes low.
Weak Reference
Weak Reference | | Object
• Object becomes eligible for garbage collection immediately after strong references disappear.
Soft Reference
A SoftReference allows an object to stay in memory until the JVM requires additional memory.
Use Cases:
• Image caches • Application caches • Frequently used data
Example: Suppose an application caches product information.
If memory is available:
• Cached objects remain.
If memory becomes low:
• JVM automatically clears them.
Code Example:
import java.lang.ref.SoftReference;
public class Demo {
public static void main(String[] args) {
String data =
new String("Cached Data");
SoftReference<String> reference =
new SoftReference<>(data);
data = null;
System.out.println(
reference.get());
}
}Possible Output:
Cached Data
The object may remain available until memory pressure occurs.
Weak Reference
A WeakReference allows an object to be garbage collected as soon as no strong references exist.
Use Cases:
• WeakHashMap • Metadata caches • Object tracking
Example:
import java.lang.ref.WeakReference;
public class Demo {
public static void main(String[] args) {
String data =
new String("Temporary Data");
WeakReference<String> reference =
new WeakReference<>(data);
data = null;
System.gc();
System.out.println(
reference.get());
}
}Possible Output:
null
The object may be collected immediately after GC runs.
Example: Suppose a web application stores user profile images.
Using SoftReference:
• Images remain cached while memory is sufficient. • Images are automatically removed when memory becomes scarce.
Using WeakReference:
• Images may disappear quickly after becoming unused.
Soft Reference vs Weak Reference
Feature Soft Reference Weak Reference
Garbage Collection When memory is low As soon as object is weakly reachable
Object Lifetime Longer Shorter
Caching Suitability Excellent Limited
Memory Sensitivity High Very High
Typical Usage Caches Lookup tables
Common Real-World Usage
SoftReference:
• Image caching • Database query caching • Frequently accessed objects
WeakReference:
• WeakHashMap • Listener management • Metadata storage • Object tracking frameworks
WeakHashMap Example
WeakHashMap internally uses weak references for keys.
Benefit:
When a key is no longer referenced elsewhere:
• The key-value entry is automatically removed.
This helps prevent memory leaks.
When to Use Which?
Use SoftReference:
• When objects should remain available as long as memory permits. • For cache implementations.
Use WeakReference:
• When objects should disappear immediately after becoming unused. • For temporary associations and lookup structures.
Interview Tip: A concise interview answer is:
"SoftReference and WeakReference are special reference types used for memory-sensitive applications. A SoftReference keeps an object alive until the JVM needs memory, making it suitable for caches. A WeakReference allows an object to be garbage collected as soon as no strong references exist, making it useful for lookup tables, WeakHashMap, and preventing memory leaks."