您的位置:首页 > 移动开发 > Android开发

Android实战技巧之三十八:Handler使用中可能引发的内存泄漏

2015-07-14 11:20 621 查看

问题描述

曾几何时,我们用原来的办法使用Handler时会有下面一段温馨的提示:

This Handler class should be static or leaks might occur


下面是更详细的说明(Android Studio上的警告,不知道Eclipse上是否相同)

Since this Handler is declared as an inner class, it may prevent the outer class from being garbage collected. If the Handler is using a Looper or MessageQueue for a thread other than the main thread, then there is no issue. If the Handler is using the Looper or MessageQueue of the main thread, you need to fix your Handler declaration, as follows: Declare the Handler as a static class; In the outer class, instantiate a WeakReference to the outer class and pass this object to your Handler when you instantiate the Handler; Make all references to members of the outer class using the WeakReference object.

大概意思就是:

一旦Handler被声明为内部类,那么可能导致它的外部类不能够被垃圾回收。如果Handler是在其他线程(我们通常成为worker thread)使用Looper或MessageQueue(消息队列),而不是main线程(UI线程),那么就没有这个问题。如果Handler使用Looper或MessageQueue在主线程(main thread),你需要对Handler的声明做如下修改:

声明Handler为static类;在外部类中实例化一个外部类的WeakReference(弱引用)并且在Handler初始化时传入这个对象给你的Handler;将所有引用的外部类成员使用WeakReference对象。

解决方案一

上面的描述中基本上把推荐的修改方法明确表达了出来,下面的代码是我自己使用中的一个实现,请参考:

private CopyFileHandler mHandler;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_appstart);
    mHandler = new CopyFileHandler(this);
    startCopyDBThread();
}

private void startCopyFileThread(){
    Log.d(TAG, "startCopyDBThread");
    new Thread(new Runnable() {
        @Override
        public void run() {
            //DO SOMETHING LIKE: copyDBFile();
            Message msg=mHandler.obtainMessage();
            mHandler.sendMessage(msg);
        }
    }).start();
}

private static class CopyFileHandler extends Handler {
    WeakReference<AppStartActivity> mActivity;
    public CopyFileHandler(AppStartActivity activity) {
        mActivity = new WeakReference<>(activity);
    }

    public void handleMessage(Message msg) {
        final AppStartActivity activity = mActivity.get();
       //handle you message here!
    }
}


为什么会内存泄漏

那么为什么不这样做会引发内存泄漏呢?

这与几个关键词有关:内部类、Handler的消息循环(Looper)、Java垃圾回收机制。

需要强调一下,并不是每次使用Handler都会引发内存泄漏,这里面有一定的几率,需要满足特定条件才会引起泄漏。

内部类会有一个指向外部类的引用。

垃圾回收机制中约定,当内存中的一个对象的引用计数为0时,将会被回收。

Handler作为Android上的异步消息处理机制(好吧,我大多用来进行worker thread与UI线程同步),它的工作是需要Looper和MessageQueue配合的。简单的说,要维护一个循环体(Looper)处理消息队列(MessageQueue)。每循环一次就从MessageQueue中取出一个Message,然后回调相应的消息处理函数。

如果,我是说如果,循环体中有消息未处理(Message排队中),那么Handler会一直存在,那么Handler的外部类(通常是Activity)的引用计数一直不会是0,所以那个外部类就不能被垃圾回收。很多人会遇到activity的onDestroy方法一直不执行就是这个原因。

另一个解决方案的尝试

警告描述中提到了Handler在worker thread中使用Looper或MessageQueue,我尝试了一下,请大家品鉴。

private Handler testHandler;
    private Thread mThread = new Thread() {
        public void run() {
            Log.d(TAG,"mThread run");
            Looper.prepare();
            testHandler = new Handler() {
                public void handleMessage(Message msg) {
                    Log.d("TAG", "worker thread:"+Thread.currentThread().getName());
                    switch (msg.what) {
                        //handle message here
                    }
                }
            };
            Looper.loop();
        }
    };

    //start thread here
    if(Thread.State.NEW == mThread.getState()) {
        Log.d(TAG, "mThread name: " + mThread.getName());
        mThread.start();
    }

    //send message here
    testHandler.sendEmptyMessage(1);


参考:

http://stackoverflow.com/questions/11407943/this-handler-class-should-be-static-or-leaks-might-occur-incominghandler

http://m.blog.csdn.net/blog/wurensen/41907663

/article/1336242.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: