zoukankan      html  css  js  c++  java
  • Java内部类持有外部类的引用详细分析与解决方案

    Java中内部类的定义与使用一般为成员内部类与匿名内部类,他们的对象都会隐式持有外部类对象的引用,影响外部类对象的回收。

    GC只会回收没有被引用或者根集不可到达的对象(取决于GC算法),内部类在生命周期内始终持有外部类的对象的引用,造成外部类的对象始终不满足GC的回收条件,反映在内存上就是内存泄露。(如,Android中Activity的内存泄露)

    解决方案为

    1.将内部类定义为static

    2.用static的变量引用匿名内部类的实例

    测试代码

    1. class Outer {  
    2.     class Inner {  
    3.         public String publicString = "Inner.publicString";  
    4.     }  
    5.   
    6.     Other anonymousOther = new Other() {  
    7.         public String publicString = "Anonymous Other.publicString";  
    8.     };  
    9.     public Other getAnonymousOther() {  
    10.         return anonymousOther;  
    11.     }  
    12.   
    13.     Other Other = new Other();  
    14.     public Other getOther() {  
    15.         return Other;  
    16.     }  
    17. }  
    18.   
    19. class Other {  
    20.     public String publicString = "Other.publicString";  
    21. }  

    调用代码

    1. public static void main(String args[]) {  
    2.         printField(new Outer().new Inner());  
    3.         System.out.println(" ");  
    4.         printField(new Outer().getAnonymousOther());  
    5.         System.out.println(" ");  
    6.         printField(new Outer().getOther());  
    7.     }  

    测试结果

    1. Class: at.miao.Outer$Inner  
    2. 变量: publicString 值为 Inner.publicString  
    3. 变量: this$0 值为 at.miao.Outer@456c5f50  
    4.       
    5. Class: at.miao.Outer$1  
    6. 变量: publicString 值为 Anonymous Other.publicString  
    7. 变量: this$0 值为 at.miao.Outer@10e80317  
    8. Class: at.miao.Other  
    9. 变量: publicString 值为 Other.publicString  


    可以看到内部类与匿名内部类的实例都有一个外部类类型的名为this$0的变量指向了外部类的对象。

    加上static之后,代码为

    1. class Outer {  
    2.     static class Inner {  
    3.         public String publicString = "Inner.publicString";  
    4.     }  
    5.   
    6.     static Other anonymousOther = new Other() {  
    7.         public String publicString = "Anonymous Other.publicString";  
    8.     };  
    9.   
    10.     public Other getAnonymousOther() {  
    11.         return anonymousOther;  
    12.     }  
    13.   
    14.     Other Other = new Other();  
    15.     public Other getOther() {  
    16.         return Other;  
    17.     }  
    18. }  
    19.   
    20. class Other {  
    21.     public String publicString = "Other.publicString";  
    22. }  

    调用代码

    1. public static void main(String args[]) {  
    2.         printField(new Outer.Inner());  
    3.         System.out.println(" ");  
    4.         printField(new Outer().getAnonymousOther());  
    5.         System.out.println(" ");  
    6.         printField(new Outer().getOther());  
    7.     }  

    测试结果

    1. Class: at.miao.Outer$Inner  
    2. 变量: publicString 值为 Inner.publicString  
    3.       
    4. Class: at.miao.Outer$1  
    5. 变量: publicString 值为 Anonymous Other.publicString  
    6.       
    7. Class: at.miao.Other  
    8. 变量: publicString 值为 Other.publicString  

    可以看到静态内部类实例、static引用的匿名内部类的实例未引用外部类的实例。

  • 相关阅读:
    WordCount C语言实现求文本的字符数,单词数,行数
    iOS 数据持久化
    Apple store加急审核
    iOSTableViewCell不等高的几种方法
    Xcode 插件(时间就是生命)
    iOS UI组件汇总
    iOS之获取经纬度并通过反向地理编码获取详细地址
    iOS 开发常用宏
    iOS让你的app一直在后台活着(运行)
    OC动画——基础动画CABasicAnimation使用
  • 原文地址:https://www.cnblogs.com/ldq2016/p/6626670.html
Copyright © 2011-2022 走看看