Android Handler内存泄漏

1. 原因分析

1.1 内存泄漏实例

首先看一个简单的Handler引发内存泄漏的例子:

public class HandlerActivity extends Activity {

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case Const.NOTIFY_LOCATION_SUC:
                    boolean isSuc = (Boolean) msg.obj;
                    doLocation(isSuc);
                    break;

                default:
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();

 mHandler.sendEmptyMessageDelayed(Const.NOTIFY_LOCATION_SUC, 10000);
    }

}

1.2 内存泄漏分析

Handler的引用阻止GC回收Activity

  • 在Java中,非静态(匿名)内部类会默认隐性引用外部类对象。
    如果外部类是Activity就会造成内存泄漏

  • Handler和Activity的生命周期不一致
    就上面的例子而言,当Activity结束后,消息循环中还有延时消息,那么Handler还会一直存在,知道所有消息处理完,但Handler引用了Activity,那么就会阻止Activity被回收,造成内存泄漏。


2. 解决方式

(1)使用显式引用

  • 把Handler声明为静态内部类,而静态内部类不会引用外部类对象
  • 把Handler声明为静态外部类

(2)在handler中弱引用Activity

public class HandlerActivity extends Activity {
    private static final int MSG_START_RECORDING = 0x1001;
    private static final int MSG_STOP_RECORDING = 0x1002;

    private WorkHandler mHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mHandler = new WorkHandler(this);
    }

    public void start() {
        mHandler.sendMessage(mHandler.obtainMessage(MSG_START_RECORDING));
    }

    public void stop() {
        mHandler.sendMessage(mHandler.obtainMessage(MSG_STOP_RECORDING));
    }

    private static class WorkHandler extends Handler {
        private WeakReference<HandlerActivity> mWeakActivity;

        public WorkHandler(HandlerActivity activity) {
            super();
            mWeakActivity = new WeakReference<HandlerActivity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            HandlerActivity activity = mWeakActivity.get();

            switch (msg.what) {
                case MSG_START_RECORDING:
                    activity.handleStartEncoding();
                    break;

                case MSG_STOP_RECORDING:
                    activity.handleStopEncoding();
                    break;
            }
        }
    }

    private void handleStartEncoding() {

    }

    private void handleStopEncoding() {

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值