Does an outer class get GCed if I maintain a reference to an inner class?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Nik_West
Posted On:   Thursday, August 2, 2001 01:07 PM

In order to call back into some objects I store anonymous inner classes in a Set in some other object. I want the inner classes to be garbage collected once there is no reference anymore to the outer class from another object. How is garbage collection handled? Here are my assumptions: The set maintains a reference to the inner class, the inner class has a reference to the outer class. Result: The outer class gets never garbage collected. bad If I maintain WeakReferences of the anonymous classes in the Set, they get garbage collected, even if the outer class is still referenced somewhere else. bad So here is my q   More>>

In order to call back into some objects I store anonymous inner classes in a Set in some other object. I want the inner classes to be garbage collected once there is no reference anymore to the outer class from another object.


How is garbage collection handled?


Here are my assumptions:


The set maintains a reference to the inner class, the inner class has a reference to the outer class. Result:
The outer class gets never garbage collected.


bad


If I maintain WeakReferences of the anonymous classes in the Set, they get garbage collected, even if the outer class is still referenced somewhere else.


bad


So here is my question:


Are my assumptions correct? If yes, what is the correct way to solve my problem?


If no, what are the correct assumptions?


Thanks



Dominik

   <<Less

Re: Does an outer class get GCed if I maintain a reference to an inner class?

Posted By:   Christopher_Schultz  
Posted On:   Friday, August 3, 2001 12:48 PM

The Java GC is pretty good about figuring out circular dependancies in objects.



If object A has a reference to object B, and object B has a reference to object A, but neither of them are referenced by any live thread, they'll both get whacked sooner or later. Granted, the fact that the reference each other will trip up the GC a little more than, say, a simpler set of circumstances, and they might not get collected at the first run of the GC. Then again, you'd never know, anyway, because the GC is so fickle in the first place.



My guess you'll be fine without going through too much trouble, like the WeakReference scheme.



-chris
About | Sitemap | Contact