zoukankan      html  css  js  c++  java
  • This Handler class should be static or leaks might occur,Handler和Context使用的注意事项!

    Android中。在使用到Handler的时候,假设按例如以下代码编写:

    	private Handler handler;
    	
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		handler = new Handler() {
    
    			@Override
    			public void handleMessage(Message msg) {
    				// TODO Auto-generated method stub
    				super.handleMessage(msg);
    			}
    
    		};
    
    		handler.sendMessageDelayed(Message.obtain(), 60000);
    
    		// just finish this activity
    		finish();
    	}

    然后执行Android Lint工具会有一个内存泄露警告:

    This Handler class should be static or leaks might occur (com.example.ta.MainActivity.1)

    Issue: Ensures that Handler classes do not hold on to a reference to an outer class
    Id: HandlerLeak

    In Android, Handler classes should be static or leaks might occur. Messages enqueued on the application thread’s MessageQueue also retain their target Handler. If the Handler is an inner class, its outer class will be retained as well. To avoid leaking the outer class, declare the Handler as a static nested class with a WeakReference to its outer class.

    原因是:

    1. 当Android应用启动的时候,会先创建一个应用主线程的Looper对象,Looper实现了一个简单的消息队列,一个一个的处理里面的Message对象。

      主线程Looper对象在整个应用生命周期中存在。

    2. 当在主线程中初始化Handler时,该Handler和Looper的消息队列关联。发送到消息队列的Message会引用发送该消息的Handler对象,这样系统能够调用 Handler#handleMessage(Message) 来分发处理该消息。

    3. 在Java中,非静态(匿名)内部类会引用外部类对象。而静态内部类不会引用外部类对象。

    4. 假设外部类是Activity。则会引起Activity泄露 。

    当Activity finish后,延时消息会继续存在主线程消息队列中1分钟。然后处理消息。而该消息引用了Activity的Handler对象,然后这个Handler又引用了这个Activity。这些引用对象会保持到该消息被处理完。这样就导致该Activity对象无法被回收,从而导致了上面说的 Activity泄露。

    要改动该问题,仅仅须要依照Lint提示的那样。把Handler类定义为静态就可以,然后通过WeakReference 来保持外部的Activity对象。


    	private static class WebViewHandler extends Handler{
    		
    		public static final int what_pullRefreshCompleted = 1;
    
    		private final WeakReference<WebActivity> mActivity;
    		
    		public WebViewHandler(WebActivity mActivity) {
    			super();
    			this.mActivity = new WeakReference<WebActivity>(mActivity);
    		}
    
    		@Override
    		public void handleMessage(Message msg) {
    			if(mActivity.get() == null)
    				return;
    			switch(msg.what){
    				case what_pullRefreshCompleted:
    					if (mActivity.get().getmPullRefreshWebView() != null)//由于该Handler为static,所以我们通过调用方法来获取外部类属性
    						mActivity.get().getmPullRefreshWebView().refreshComplete();
    					break;
    			}
    			super.handleMessage(msg);
    		}
    	}

    所以,在Activity中使用内部类的时候,须要时刻考虑您能否够控制该内部类的生命周期,假设不能够,则最好定义为静态内部类。



    另外。再附上一个注意事项知识点,希望对大家实用:

    不要持有 Context 的静态引用!

    public class ExampleActivity extends Activity {
    
    	//...
    	private static MyAccountInfo info;
    	//...
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		//...
    		info = new MyAccountInfo(this);
    		//...
    	}
    
    }
    上面这点代码。对于一个有丰富经验的人来,可能不会犯这种错误。

    然后也会有非常多人会犯这种错误,一段看似没有问题的代码,实际上可能会给我们的应用带来麻烦。不要这样做,可能会出问题的。

    假设 MyAccountInfo 通过它的构造函数保持一个指向Activity的引用,这个Activity将不会被垃圾回收(GC)。除非静态变量被从新分配到不同的Activity。

    这是由于 info 是静态变量,而静态变量的内存是不会被回收,直到应用程序退出才回收。假设你正在试图做这种事情。你的代码非常有可能有严重的错误。依据实际逻辑须要换个写法吧。

    注:从技术上说,你能够对一个Application Context进行静态变量引用而不引起内存泄露。但我不建议你这样做。







  • 相关阅读:
    SGU 271 Book Pile (双端队列)
    POJ 3110 Jenny's First Exam (贪心)
    HDU 4310 Hero (贪心)
    ZOJ 2132 The Most Frequent Number (贪心)
    POJ 3388 Japanese Puzzle (二分)
    UVaLive 4628 Jack's socks (贪心)
    POJ 2433 Landscaping (贪心)
    CodeForces 946D Timetable (DP)
    Android Studio教程从入门到精通
    Android Tips – 填坑手册
  • 原文地址:https://www.cnblogs.com/yxysuanfa/p/6755631.html
Copyright © 2011-2022 走看看