Skip to content

Commit 7741613

Browse files
committed
feat(WeakReferenceDemo): add demo of WeakReference behavior and GC collection
What - Added `WeakReferenceDemo` class. - Created `Phone` object with a strong reference. - Wrapped object in a `WeakReference<Phone>`. - Printed object before and after nullifying strong reference and invoking GC. Why - To demonstrate how weak references behave in Java. - Show that weak references do not prevent GC from reclaiming an object. - Illustrate memory-sensitive referencing useful for caches and temporary objects. How - Step 1: Create strong reference `phone` → new Phone("Apple", "16 Pro"). - Step 2: Wrap in WeakReference (`phoneWeakReference`). - Step 3: Print value via weak reference before GC. - Step 4: Nullify strong reference (`phone = null`). - Step 5: Call `System.gc()` and wait briefly. - Step 6: Print value via weak reference again (likely null). Logic - Inputs: - Phone constructor with brand and model. - Outputs: - Console log showing object before GC. - Console log showing null after GC (if collected). - Flow: 1. Strong + weak references exist. 2. Strong reference nulled → only weak reference remains. 3. GC may reclaim object and clear weak reference. 4. `get()` returns null when object collected. - Constraints: - GC timing is not deterministic; output may vary. - Complexity: - O(1) for WeakReference operations. Real-life applications - Weak references prevent memory leaks in caches, maps, and listeners. - Commonly used in `WeakHashMap`. - Useful when objects should be reclaimed automatically under memory pressure. Notes - Strong references prevent GC, weak references do not. - WeakReference `.get()` may return null anytime after GC. - Demonstrates difference between strong and weak references clearly. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 28d7664 commit 7741613

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.lang.ref.WeakReference;
2+
3+
public class WeakReferenceDemo {
4+
public static void main(String[] args) {
5+
// Create a strong reference to a Phone object.
6+
Phone phone = new Phone("Apple", "16 Pro");
7+
8+
// Wrap the Phone object inside a WeakReference.
9+
WeakReference<Phone> phoneWeakReference = new WeakReference<>(phone);
10+
11+
// At this point:
12+
// strong reference -> 'phone'
13+
// weak reference -> 'phoneWeakReference'
14+
System.out.println("Before GC: " + phoneWeakReference.get());
15+
16+
// Remove the strong reference. Now only weak reference exists.
17+
phone = null;
18+
19+
// Request Garbage Collection.
20+
System.gc();
21+
22+
try {
23+
Thread.sleep(3000); // Give GC time to run.
24+
} catch (InterruptedException e) {
25+
e.printStackTrace();
26+
}
27+
28+
// WeakReference may have been cleared by GC.
29+
System.out.println("After GC: " + phoneWeakReference.get());
30+
}
31+
}
32+
33+
// Demonstrates how weak references work in Java using WeakReference.
34+
// Weak references do not prevent an object from being garbage collected.

0 commit comments

Comments
 (0)