java - GC collects referenced object after return from scope -
i have question how gc works in java. consider following code:
class c1 { arraylist<int> mylist = new arraylist<int>(); public void setlist(arraylist<int> l) { mylist = l; } } func(c1 c) { arraylist<int> l1 = new arraylist<int>(); l1.add(1); c.setlist(l1); } main() { c1 c = new c1(); func(c); ... } my question is: gc releases 'l1' after func() returns or not?
no, doesn't, because there's root reference (stack variable c) has strong reference (mylist), new arraylist. after main() returns, c1 , arraylist collectible, because root reference disappears.
Comments
Post a Comment