• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regular inner classes & garbage collection

 
Dariusz Kordonski
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens, if an instance of an outer class becomes gc-eligible but there are instances of its inner class (in relation with this particular outer class instance) that are still referenced from stack or other objects? Is it possible at all?
 
Henry Wong
author
Marshal
Pie
Posts: 21518
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about it. To be GC eligible, an object can't be reachable. How do you propose that an object be GC eligible, if it is still reachable from an inner class object?

Henry
 
Dariusz Kordonski
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the problem is that there are a lot of sources that define 'being reachable' as 'there is somewhere a reference variable that points to this object'. And this is the way I always thought of it. And since there may be no direct reference variable pointing to outer class within the inner class, according to such definition it could be possible for an outer class instance to be gc-eligible. But, of course, if we think of being reachable in general sense, it is always possible to reach outer class with 'OuterClass.this' from within inner class... are there any more cases when 'object is not reachable != 'there is no reference variable pointing to it' ?
 
Rob Spoor
Sheriff
Pie
Posts: 20671
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An instance of an inner class has a reference to the outer class (available through OuterClass.this). So if the instance of the outer class is gc-eligible, then the inner class instance is gc-eligible by definition. If the inner class instance is not gc-eligible then neither is the outer class instance.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic