Commit e949956
committed
feat(GCDemo1): add demo of WeakReference, strong refs, and GC with ReferenceQueue
What
- Added `GCDemo1` class with nested `Big` class (allocates ~1MB array).
- Created:
• Strong reference → Big#1
• WeakReference (weak1) → Big#2 (no strong refs)
• WeakReference (weak2) → Big#1 (also held by strong initially)
- Used `ReferenceQueue<Big>` to capture cleared weak references.
- Printed object states before and after `System.gc()` and checked queue.
Why
- To demonstrate how JVM garbage collector handles strong vs weak references.
- Showed how weak references are cleared when no strong refs exist.
- Illustrated how ReferenceQueue helps detect when referents are reclaimed.
- Useful for cache design and avoiding memory leaks.
How
- Before GC:
strong → Big#1
weak1.get() → Big#2
weak2.get() → Big#1
refQueue empty
- After nulling strong and running GC:
weak1.get() → null (Big#2 reclaimed)
weak2.get() → null (Big#1 reclaimed)
refQueue contains [weak1, weak2]
- Printed enqueued weak refs from refQueue.
Logic
- Inputs:
- Instantiated Big objects with ids 1 and 2.
- Outputs:
- Console logs showing object states before/after GC.
- Enqueued references from ReferenceQueue.
- Flow:
1. Create strong + weak refs.
2. Print pre-GC states.
3. strong = null.
4. Call System.gc() and sleep briefly.
5. Print post-GC states.
6. Drain ReferenceQueue to show cleared refs.
- Constraints:
- GC behavior not strictly guaranteed; System.gc() is only a hint.
- Complexity:
- O(1) reference checks, O(n) to drain queue (n = cleared refs).
Real-life applications
- WeakReference used in caches where objects can be reclaimed automatically.
- Helps avoid memory leaks when object lifetime should not be strictly managed.
- ReferenceQueue allows cleanup tasks when objects are collected.
Notes
- Strong refs keep objects alive.
- Weak refs do not prevent GC.
- ReferenceQueue signals GC-cleared refs.
- Demo shows memory pressure handling and lifecycle of weak references.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 2fb08a5 commit e949956
File tree
1 file changed
+139
-0
lines changed- Section 25 Collections Frameworks/Map Interface/Garbage Collection/src
1 file changed
+139
-0
lines changedLines changed: 139 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
0 commit comments