Practice ยท 1 of 3
Watch the collector retire data
1. static int retires() โ create THREE distinct Object values, wrap each in a
WeakReference, do NOT keep strong references, call System.gc() (twice, for
reliability), then count how many weak refs return null from get().
2. static int retiresWithStrong() โ same three objects, but keep them in a
static List<Object> pinned first; after System.gc() count nulls (expect 0 โ
strong refs are immortal while reachable).
3. static long heapAddress() โ measure USED heap as
totalMemory() - freeMemory() BEFORE and AFTER allocating int[1_000_000];
return the difference (after - before). Allocation shows up as used-memory growth.
Difficulty: advanced
Press Submit to check your solution.
Back to lesson: Practice: Memory drills