非静态的内部类会持有外部类的一个隐式引用

转载地址: http://blog.csdn.net/zhongwcool/article/details/22886067


另外附上一个正在看的内存泄露的技术文,附上链接:https://yq.aliyun.com/articles/3009?spm=5176.100240.searchblog.31.oDWxG1



Activity是如何泄漏的

只要非静态的匿名类对象没有被回收,MainActivity就不会被回收,MainActivity所关联的资源和视图都不会被回收,发生比较严重的内存泄漏。

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * Example illustrating how threads persist across configuration 
  3.  * changes (which cause the underlying Activity instance to be 
  4.  * destroyed). The Activity context also leaks because the thread 
  5.  * is instantiated as an anonymous class, which holds an implicit 
  6.  * reference to the outer Activity instance, therefore preventing 
  7.  * it from being garbage collected. 
  8.  */  
  9. public class MainActivity extends Activity {  
  10.    
  11.   @Override  
  12.   protected void onCreate(Bundle savedInstanceState) {  
  13.     super.onCreate(savedInstanceState);  
  14.     exampleOne();  
  15.   }  
  16.    
  17.   private void exampleOne() {  
  18.     newThread() {//匿名内部类,非静态的匿名类会持有外部类的一个隐式引用  
  19.       @Override  
  20.       publicvoid run() {  
  21.         while(true) {  
  22.           SystemClock.sleep(1000);  
  23.         }  
  24.       }  
  25.     }.start();  
  26.   }  
  27. }  
要解决MainActivity的内存泄漏问题,只需把非静态的Thread匿名类定义成静态的内部类就行了(静态的内部类不会持有外部类的一个隐式引用):

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * This example avoids leaking an Activity context by declaring the 
  3.  * thread as a private static inner class, but the threads still 
  4.  * continue to run even across configuration changes. The DVM has a 
  5.  * reference to all running threads and whether or not these threads 
  6.  * are garbaged collected has nothing to do with the Activity lifecycle. 
  7.  * Active threads will continue to run until the kernel destroys your 
  8.  * application's process. 
  9.  */  
  10. public class MainActivity extends Activity {  
  11.    
  12.   @Override  
  13.   protected void onCreate(Bundle savedInstanceState) {  
  14.     super.onCreate(savedInstanceState);  
  15.     exampleTwo();  
  16.   }  
  17.    
  18.   private void exampleTwo() {  
  19.     new MyThread().start();  
  20.   }  
  21.    
  22.   private static class MyThread extends Thread {  
  23.     @Override  
  24.     public void run() {  
  25.       while(true) {  
  26.         SystemClock.sleep(1000);  
  27.       }  
  28.     }  
  29.   }  
  30. }  
现在新创建的Thread不会持有MainActivity的一个隐式引用,当手机屏幕旋转时不会阻止垃圾回收器对旧MainActivity的回收工作。

Thread是如何泄漏的

在提到的第二个问题中,一旦一个新的Activity创建,那么就有一个Thread永远得不到清理回收,发生内存泄漏。Threads在Java中是GC roots;意味着  Dalvik Virtual Machine (DVM) 会为所有活跃的threads在运行时系统中保持一个硬引用,这会导致threads一直处于运行状态,垃圾收集器将永远不可能回收它。出于这个原因,我们应当为threads添加一个结束的逻辑,如下代码所示
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * Same as example two, except for this time we have implemented a 
  3.  * cancellation policy for our thread, ensuring that it is never 
  4.  * leaked! onDestroy() is usually a good place to close your active 
  5.  * threads before exiting the Activity. 
  6.  */  
  7. publicclass MainActivity extends Activity {  
  8.   privateMyThread mThread;  
  9.    
  10.   @Override  
  11.   protectedvoid onCreate(Bundle savedInstanceState) {  
  12.     super.onCreate(savedInstanceState);  
  13.     exampleThree();  
  14.   }  
  15.    
  16.   privatevoid exampleThree() {  
  17.     mThread = new MyThread();  
  18.     mThread.start();  
  19.   }  
  20.    
  21.   /** 
  22.    * Static inner classes don't hold implicit references to their 
  23.    * enclosing class, so the Activity instance won't be leaked across 
  24.    * configuration changes. 
  25.    */  
  26.   privatestatic class MyThread extends Thread {  
  27.     privateboolean mRunning = false;  
  28.    
  29.     @Override  
  30.     publicvoid run() {  
  31.       mRunning = true;  
  32.       while(mRunning) {  
  33.         SystemClock.sleep(1000);  
  34.       }  
  35.     }  
  36.    
  37.     publicvoid close() {  
  38.       mRunning = false;  
  39.     }  
  40.   }  
  41.    
  42.   @Override  
  43.   protectedvoid onDestroy() {  
  44.     super.onDestroy();  
  45.     mThread.close();  
  46.   }  
  47. }  


在上述的代码中,当Activity结束销毁时在onDestroy()方法中结束了新创建的线程,保证了thread不会发生泄漏。但是如果你想在手机配置发生改变时保持原有的线程而不重新创建的话,你可以考虑使用fragment来保留原有的线程,以备下一次使用具体做法可以参考我之前的一篇文章  http://blog.csdn.net/tu_bingbing/article/details/9274289,关于这方面  APIdemos  中也做了相关的实现。 

结论


在Android中,长时间运行的任务和Acyivity生命周期进行协调会有点困难,如果你不加以小心的话会导致内存泄漏。关于如何处理这个棘手的问题,下面有几个基本的技巧供参考

       1、使用静态内部类/匿名类,不要使用非静态内部类/匿名类.非静态内部类/匿名类会隐式的持有外部类的引用,外部类就有可能发生泄漏。而静态内部类/匿名类不会隐式的持有外部类引用,外部类会以正常的方式回收,如果你想在静态内部类/匿名类中使用外部类的属性或方法时,可以显示的持有一个弱引用。

     2、不要以为Java永远会帮你清理回收正在运行的threads.在上面的代码中,我们很容易误以为当Activity结束销毁时会帮我们把正在运行的thread也结束回收掉,但事情永远不是这样的!Java threads会一直存在,只有当线程运行完成或被杀死掉,线程才会被回收。所以我们应该养成为thread设置退出逻辑条件的习惯。

     3、适当的考虑下是否应该使用线程.Android应用框架设计了许多的类来简化执行后台任务,我们可以使用与Activity生命周期相关联的Loaders来执行简短的后台查询任务。如果一个线程不依赖与Activity,我们还可以使用Service来执行后台任务,然后用BroadcastReceiver来向Activity报告结果。另外需要注意的是本文讨论的thread同样使用于AsyncTasks,AsyncTask同样也是由线程来实现,只不过使用了Java5.0新增并发包中的功能,但同时需要注意的是根据官方文档所说,AsyncTask适用于执行一些简短的后台任务
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值