Skip to content

Commit 45c4a96

Browse files
committed
Types of References in Java.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 7e724b0 commit 45c4a96

File tree

1 file changed

+88
-0
lines changed
  • Section 25 Collections Frameworks/Map Interface/Garbage Collection/SoftReference vs WeakReference vs PhantomReference/src

1 file changed

+88
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
Types of References in Java:
2+
3+
1. Strong Reference (Default)
4+
- Example: Object obj = new Object();
5+
- As long as a strong reference exists, the object is **not eligible for GC**.
6+
- Most common type of reference.
7+
- Must be explicitly set to null (or go out of scope) before GC can reclaim it.
8+
9+
2. Soft Reference
10+
- Class: java.lang.ref.SoftReference<T>
11+
- Object will be collected **only when JVM runs low on memory**.
12+
- Used for caches where data can be recomputed or reloaded if cleared.
13+
- Example: SoftReference<Image> softImg = new SoftReference<>(new Image());
14+
15+
3. Weak Reference
16+
- Class: java.lang.ref.WeakReference<T>
17+
- Object is **collected eagerly** when no strong/soft references exist.
18+
- Commonly used in WeakHashMap (keys are weak references).
19+
- Example: WeakReference<Data> weakRef = new WeakReference<>(new Data());
20+
21+
4. Phantom Reference
22+
- Class: java.lang.ref.PhantomReference<T>
23+
- Object is **already finalized** and enqueued before GC reclaims memory.
24+
- Always returns null from get().
25+
- Must be used with a ReferenceQueue.
26+
- Useful for post-mortem cleanup (e.g., freeing native memory, file handles).
27+
28+
-----------------------------------
29+
ASCII Flow of Reachability
30+
-----------------------------------
31+
32+
Strong Ref → Object (never GC while reachable)
33+
34+
Soft Ref → Object (GC only if memory is low)
35+
36+
Weak Ref → Object (GC as soon as no strong/soft refs exist)
37+
38+
Phantom Ref→ Object (already finalized; enqueued before actual GC for cleanup)
39+
40+
-----------------------------------
41+
Quick Recap
42+
-----------------------------------
43+
✔ Strong (default, keeps object alive)
44+
✔ Soft (cache-friendly, GC only under memory pressure)
45+
✔ Weak (short-lived, GC at next cycle when no strong refs)
46+
✔ Phantom (cleanup notification before GC final removal)
47+
48+
49+
SoftReference vs WeakReference vs PhantomReference:
50+
51+
Quick Comparison Table
52+
53+
| Aspect | SoftReference | WeakReference | PhantomReference |
54+
|---------------------|-------------------------------------------------------------------------------|----------------------------------------------------------------------|----------------------------------------------------------------------------------|
55+
| Reachability | Softly reachable (strong refs gone, GC may keep if memory is available) | Weakly reachable (collected eagerly once no strong/soft refs exist) | Phantomly reachable (object finalized, only enqueued for cleanup) |
56+
| get() returns | Referent while not cleared | Referent until GC clears | Always null |
57+
| When GC clears | Under memory pressure (least eager) | On next GC cycle if no strong/soft refs | After finalization, enqueued before reclaim |
58+
| Typical use-case | Memory-sensitive caches (e.g., image cache) | WeakHashMap keys, canonical maps, event listeners | Native resource cleanup, precise post-mortem cleanup |
59+
| ReferenceQueue need | Optional (often not used) | Optional (sometimes used for notification) | Mandatory (always used to detect object reclaim and trigger cleanup) |
60+
61+
---
62+
63+
SoftReference:
64+
strong -> null
65+
soft -> [Object] (kept until memory is low)
66+
GC under pressure -> clear soft -> object reclaimed
67+
68+
69+
WeakReference:
70+
strong -> null
71+
soft -> null
72+
weak -> [Object]
73+
GC runs -> weak cleared -> object reclaimed
74+
75+
PhantomReference:
76+
strong -> null
77+
soft -> null
78+
weak -> null
79+
phantom-> (no direct access)
80+
After finalization → phantom enqueued in ReferenceQueue → cleanup action
81+
82+
• SoftReference → use for caches, but JVM heuristics vary.
83+
In practice, bounded caches (LRU via LinkedHashMap) are more predictable.
84+
85+
• WeakReference → great for WeakHashMap (keys automatically cleared when no strong ref exists).
86+
Avoid memory leaks in registries/listeners.
87+
88+
• PhantomReference → best for precise cleanup of native resources. Always pair with a ReferenceQueue.

0 commit comments

Comments
 (0)