码迷,mamicode.com
首页 > 移动开发 > 详细

Android中使用Handler造成内存泄露的分析和解决

时间:2015-06-10 14:11:30      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

Handler 内部类持有 外部类Activity的引用,如果Activity退出而Handler还有延迟处理的消息没有处理完,会导致Activity不能回收,反复如此会导致内存泄露。

解决方案一: mHandler.removeCallbacksAndMessages(null); // 参数为null时会清除所有消息。

解决方案二:声明Handler为static并持有Activity的弱引用。关键看程序中注释的地方

public class MainActivity extends Activity {

    private static final String tag = "MainActivity";

    private TextView mTvShow;

    private MyHandler mHandler;

    private static final int DELAY_TIME = 10000; // set to 5s and 10s to check result.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mHandler = new MyHandler(MainActivity.this);

        Log.e(tag, "onCreate");

        mTvShow = (TextView)findViewById(R.id.tv_show);

        new Thread() {
            @Override
            public void run() {

                mHandler.sendEmptyMessageDelayed(1, DELAY_TIME);
                Log.e(tag, "msg send");
            }
        }.start();

    }

    @Override
    protected void onDestroy() {
        Log.e(tag, "onDestroy");
        super.onDestroy();
    }

    static class MyHandler extends Handler{

        WeakReference<MainActivity> mWeakActivity;

        public MyHandler(MainActivity activity) {
            mWeakActivity = new WeakReference<>(activity);
        }

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

            MainActivity activity = mWeakActivity.get();

            Log.e(tag, "handleMessage," + msg.what);
            // 5s is not null, 10s is null, tell that activity is recycled.
            if(null == activity){
                Log.e(tag, "null");
            }else{
                Log.e(tag, "not null");
                activity.mTvShow.setText("delay show");
            }
        }
    }
}



Android中使用Handler造成内存泄露的分析和解决

标签:

原文地址:http://blog.csdn.net/torvalbill/article/details/46440971

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!