An inner class holds a reference to the object instance that created it; thus, in the following:
public class Outer { public class Inner { } Inner i = new Inner(); } Outer o = new Outer; Outer.Inner oi = o.i; o = null; // o is still alive because oi references it oii has a hidden reference (the "outer this") to the enclosing class instance o. So long as somebody holds a reference to the Inner object, the reference to the outer object remains alive. If you mark the enclosing class or enclosing method as static, the inner class will be a static inner class, which holds no reference to the enclosing class (and also therefore lacks the ability to reference fields and methods of the outer class). Event listeners were a common source of leaks (meaning, longer-lived objects than intended) in Swing code back in the day. Might try hunting around that way on Google--there used to be a great article on "four types of lapsed listeners" back then, that described all of this in more detail. Ted Neward Java, .NET, XML Services Consulting, Teaching, Speaking, Writing http://www.tedneward.com > -----Original Message----- > From: android-developers@googlegroups.com [mailto:android- > develop...@googlegroups.com] On Behalf Of Nathan > Sent: Thursday, June 17, 2010 8:44 PM > To: Android Developers > Subject: [android-developers] Avoid non-static inner classes in an > activity? > > Since I seem to have caught two activity references in a heapdump, > where the Activity is set to singleTask. > > Romain's advice on avoiding memory leaks includes: > > "Avoid non-static inner classes in an activity if you don't control > their life cycle, use a static inner class and make a weak reference > to the activity inside" > > What does this mean exactly? I can't find any examples, positive, or > negative for this rule. > > I do have some non static inner classes in my activity. > > Most of them are anonymous inner classes like this one. I see hundreds > of them in the samples: > > button.setOnClickListener(new Button.OnClickListener() { > public void onClick(View v) { > progressHorizontal.incrementSecondaryProgressBy(-1); > // Title progress is in range 0..10000 > setSecondaryProgress(100 * > progressHorizontal.getSecondaryProgress()); > } > }); > > Are anonymous inner classes okay? > > I also see something like this in the samples: > > private OnClickListener mStopRepeatingListener = new > OnClickListener() { . . . > > Are member variables points to a non static anonymous inner class > okay? I might think so because a member variable's lifecycle is > controlled by the activity's lifecycle. > > Or do I assume that all the API samples leak a lot of contexts? > > Thanks for any insights > > Nathan > > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to android- > develop...@googlegroups.com > To unsubscribe from this group, send email to > android-developers+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en