Reputation: 7347
Assume Outer
is instantiated somewhere, and after a while there are no references to it. Does the implicit reference in inner
to the Outer
instance prevent GC from running, or does the is it a "special" reference?
public class Outer {
private class Inner extends Something {}
private Inner inner = new Inner();
}
Upvotes: 1
Views: 221
Reputation: 15425
If the instance of Outer
is not reachable from a GC root, the reference to an Inner
instance in your example code will not stop the garbage collector from freeing the memory the Outer
used.
Consider this diagram:
Stack of main thread public static void main(String[] args) {
| Outer outer = new Outer();
v
Outer<--\
|\ |
v \---/
Inner
Stack of main thread outer = new Outer();
|
v
Outer<--\
|\ new|
v \---/
Inner
Outer<--\ // a reference to the Outer from the Inner
|\ old| // doesn't change the fact that the Outer
v \---/ // can't be reached from a GC root.
Inner // Thus the old Outer is eligible for collection (dead)
Upvotes: 3