在Java中内部类的定义与使用一般为成员内部类与匿名内部类,他们的对象都会隐式持有外部类对象的引用,影响外部类对象的回收。
GC只会回收没有被引用或者根集不可到达的对象(取决于GC算法),内部类在生命周期内始终持有外部类的对象的引用,造成外部类的对象始终不满足GC的回收条件,反映在内存上就是内存泄露。(如,Android中Activity的内存泄露)
解决方案为
1.将内部类定义为static
2.用static的变量引用匿名内部类的实例
测试代码
- class Outer {
- class Inner {
- public String publicString = "Inner.publicString";
- }
- Other anonymousOther = new Other() {
- public String publicString = "Anonymous Other.publicString";
- };
- public Other getAnonymousOther() {
- return anonymousOther;
- }
- Other Other = new Other();
- public Other getOther() {
- return Other;
- }
- }
- class Other {
- public String publicString = "Other.publicString";
- }
调用代码
- public static void main(String args[]) {
- printField(new Outer().new Inner());
- System.out.println(" ");
- printField(new Outer().getAnonymousOther());
- System.out.println(" ");
- printField(new Outer().getOther());
- }
测试结果
- Class: at.miao.Outer$Inner
- 变量: publicString 值为 Inner.publicString
- 变量: this$0 值为 at.miao.Outer@456c5f50
- Class: at.miao.Outer$1
- 变量: publicString 值为 Anonymous Other.publicString
- 变量: this$0 值为 at.miao.Outer@10e80317
- Class: at.miao.Other
- 变量: publicString 值为 Other.publicString
可以看到内部类与匿名内部类的实例都有一个外部类类型的名为this$0的变量指向了外部类的对象。
加上static之后,代码为
- class Outer {
- static class Inner {
- public String publicString = "Inner.publicString";
- }
- static Other anonymousOther = new Other() {
- public String publicString = "Anonymous Other.publicString";
- };
- public Other getAnonymousOther() {
- return anonymousOther;
- }
- Other Other = new Other();
- public Other getOther() {
- return Other;
- }
- }
- class Other {
- public String publicString = "Other.publicString";
- }
调用代码
- public static void main(String args[]) {
- printField(new Outer.Inner());
- System.out.println(" ");
- printField(new Outer().getAnonymousOther());
- System.out.println(" ");
- printField(new Outer().getOther());
- }
测试结果
- Class: at.miao.Outer$Inner
- 变量: publicString 值为 Inner.publicString
- Class: at.miao.Outer$1
- 变量: publicString 值为 Anonymous Other.publicString
- Class: at.miao.Other
- 变量: publicString 值为 Other.publicString
可以看到静态内部类实例、static引用的匿名内部类的实例未引用外部类的实例。