If my reference to obj1 goes out of
scope, but my reference to obj2 does
not, is obj1 still eligible for
garbage collection, or is there some
dependency issues because there is
still a reference to the List
object?
If I understand you correctly you mean both obj1
and obj2
are of type List<string>
and both point to the same List<string>
instance.
When obj1
goes out of scope, there still will be still obj2
as an active reference to the List<string>
instance, so the list cannot be garbage collected.
If obj1 was part of a reference type on the heap (i.e. one of its properties) the memory space it occupied may be garbage collected as part of the outer object. If it was just a reference on the stack, GC will not be involved since the stack will be just unwound at the end of the method call when obj1 falls out of scope.
Keep in mind that obj1 is just a reference (in a way a pointer) to an object on the heap - this object may be garbage collected only when no reference is pointing to it anymore.