分享

android Avoid Memory Leaks 内部类问题

 xue_dong5437 2011-08-17
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 referencere 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. 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多