去除Android Studio内存泄漏警告Do not place Android context classes in static fields; this is a memory leak

当你使用单例类时,Android Studio出现了Do not place Android context classes in static fields; this is a memory leak的内存泄漏警告。

public class TestManager {
    private Context mContext;

    private TestManager() {
    }

    public void init(Context context) {
        mContext = context.getApplicationContext();
        Log.d("test", "init " + mContext);
    }

    public static TestManager getInstance() {
        return Holder.INSTANCE;
    }

    private static class Holder {
        private static final TestManager INSTANCE = new TestManager();
    }
}

![在这里插入图片描述](https://img-blog.csdnimg.cn/aed015b22d774fa1b3df8864c078d9ce.png
明明用了context.getApplicationContext()为什么还会出现警告呢?其实初始化init时传入Context这种做法是不恰当的,正确应该在构造方法就传入Application Context,但是单例类的构造方法是是私有的,无法对外传入Context,怎么办呢?引用一个全局静态的Application Context。
开发过程中应尽量避免传入Context对象,单例类中的Context对象必须为Application Context,保证在整个应用生命周期内引用。如下的写法可以去掉Android Studio内存泄漏的警告:

public class TestManager {
    private Context mContext;

    private TestManager() {
        mContext = AppConfigure.getApplicationContext();
    }

    public void init() {
        Log.d("test", "init " + mContext);
    }

    public static TestManager getInstance() {
        return Holder.INSTANCE;
    }

    private static class Holder {
        private static final TestManager INSTANCE = new TestManager();
    }
}

这样Android Studio就不会出现内存泄漏的警告了,当然可以不用AppConfigure 类来获取Application Context,可以通过在自定义Application中声明一个公共静态全局的Context供其他类引用。
AppConfigure 类的代码如下:

public class AppConfigure {

    private static volatile Application application;

    public static synchronized void configure(Application application) {
        AppConfigure.application = application;
    }

    public static <T extends Application> T getApplication() {
        try {
            if (application == null) {
                synchronized (AppConfigure.class) {
                    if (application == null) {
                        @SuppressLint("PrivateApi") Class<?> activityThreadClass = Class.forName("android.app.ActivityThread");
                        Method method = activityThreadClass.getMethod("currentActivityThread");
                        Object localObject = method.invoke(null, (Object[]) null);
                        Field appField = activityThreadClass.getDeclaredField("mInitialApplication");
                        appField.setAccessible(true);
                        application = (Application) appField.get(localObject);
                        if (application == null) {
                            Method method2 = activityThreadClass.getMethod("getApplication");
                            application = (Application) method2.invoke(localObject, (Object[]) null);
                        }
                    }
                }
            }
            // noinspection unchecked
            return (T) application;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    public static Context getContext() {
        return getApplicationContext();
    }

    public static Context getApplicationContext() {
        return getApplication();
    }
}
  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

言并肃

感谢大哥支持!您的鼓励是我动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值