See how objects are created, referenced, and collected when they become unreachable.
Heap
Object B
(referenced by A)
Step 1: Empty heap and stack
Step 2: Create Object A, referenced by stack variable varA
Step 3: Create Object B, referenced by Object A
Step 4: Create Object C, referenced by stack variable varC
Step 5: Remove stack reference to A (A and B become unreachable)
Step 6: GC identifies unreachable objects - A and B are marked for collection
Step 7: GC collects unreachable objects - A and B are removed from heap
Step 8: Only Object C remains (still referenced by varC)