Java内部类持有外部类的引用详细分析与解决方案

本文探讨了Java中内部类的定义与使用方式,并详细解释了成员内部类和匿名内部类如何导致外部类对象无法被垃圾回收,进而引发内存泄漏问题。文章提供了几种避免内存泄漏的方法,包括使用static关键字等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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("\t");
        printField(new Outer().getAnonymousOther());
        System.out.println("\t");
        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("\t");
        printField(new Outer().getAnonymousOther());
        System.out.println("\t");
        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引用的匿名内部类的实例未引用外部类的实例。

将匿名内部类的实例化操作放到外部类的静态方法中也可以达到上述效果:

public static Other getAnonymousOther() {
        return new Other() {
            public String publicString = "Anonymous Other.publicString";
        };
    }

调用代码

printField(Outer.getAnonymousOther());

采用静态化的方式不一定是最好的解决方案,请根据业务需要以及代码优化需要选择。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值