Android使用View.post()方法内存泄漏

最近开发中,使用 AsyncTask + ProgressDialog 显示进度信息,但在AsyncTask停止,Activity finish 后该Activity的实例始终不会被gc,多次运行程序后,会存在多个activity,造成内存泄漏。 下面详细分析一下:

一份显示进度条的测试代码:

[java]  view plain copy
  1. public class Main extends Activity {  
  2.   
  3.   
  4.     @Override  
  5.     protected void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.   
  8.         TextView tv = new TextView(this);  
  9.         tv.setText("Init State");  
  10.         setContentView(tv);  
  11.   
  12.         tv.setOnClickListener(new OnClickListener() {  
  13.   
  14.             @Override  
  15.             public void onClick(View v) {  
  16.                 showProgress(Main.this);  
  17.             }  
  18.         });  
  19.     }  
  20.   
  21.     public void showProgress(final Activity activity) {  
  22.         new AsyncTask<Void, Void, Void>() {  
  23.             ProgressDialog progressDial;  
  24.   
  25.             protected void onPreExecute() {  
  26.                 progressDial  = new ProgressDialog(activity);  
  27.                 progressDial.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  28.                 progressDial.show();  
  29.             };  
  30.   
  31.             @Override  
  32.             protected Void doInBackground(Void... params) {  
  33.                 doSomeHeavyWork(progressDial);  
  34.                 return null;  
  35.             }  
  36.   
  37.             protected void onPostExecute(Void result) {  
  38.                 progressDial.dismiss();  
  39.             };  
  40.   
  41.         }.execute();  
  42.     }  
  43.   
  44.     void doSomeHeavyWork(ProgressDialog progress) {  
  45.         try {  
  46.             for (int i = 1; i <= 10; ++i) {  
  47.                 progress.setProgress(i);  
  48.                 Thread.sleep(1000);  
  49.             }  
  50.         } catch (Exception e) {  
  51.         }  
  52.     }  
  53. }  

上述代码发生内存泄漏的地方在 doSomeHeavyWork() 的 progress.setProgress(i); 部分;我们看一下setProgress()的实现,最终会调用ProgressBar 类的如下方法:

[java]  view plain copy
  1. private synchronized void refreshProgress(int id, int progress, boolean fromUser) {  
  2.     if (mUiThreadId == Thread.currentThread().getId()) {  
  3.         doRefreshProgress(id, progress, fromUser, true);  
  4.     } else {  
  5.         RefreshProgressRunnable r;  
  6.         if (mRefreshProgressRunnable != null) {  
  7.             // Use cached RefreshProgressRunnable if available  
  8.             r = mRefreshProgressRunnable;  
  9.             // Uncache it  
  10.             mRefreshProgressRunnable = null;  
  11.             r.setup(id, progress, fromUser);  
  12.         } else {  
  13.             // Make a new one  
  14.             r = new RefreshProgressRunnable(id, progress, fromUser);  
  15.         }  
  16.         post(r);  
  17.     }  
  18. }  
[java]  view plain copy
  1.     private class RefreshProgressRunnable implements Runnable {  
  2.   
  3.         private int mId;  
  4.         private int mProgress;  
  5.         private boolean mFromUser;  
  6.   
  7.         RefreshProgressRunnable(int id, int progress, boolean fromUser) {  
  8.             mId = id;  
  9.             mProgress = progress;  
  10.             mFromUser = fromUser;  
  11.         }  
  12.   
  13.         public void run() {  
  14.             doRefreshProgress(mId, mProgress, mFromUser, true);  
  15.             // Put ourselves back in the cache when we are done  
  16.             mRefreshProgressRunnable = this;  
  17.         }  
  18.   
  19.         public void setup(int id, int progress, boolean fromUser) {  
  20.             mId = id;  
  21.             mProgress = progress;  
  22.             mFromUser = fromUser;  
  23.         }  
  24.     }  

if 语句表明当调用的该方法的线程是UI线程时,则直接执行doRefreshProgress() 方法以刷新界面;否则,创建一个RefreshProgressRunnable,并通过调用View.pos()方法将其插入到UI线程的消息队列中。 View.post()实现如下:

[java]  view plain copy
  1. public boolean post(Runnable action) {  
  2.     Handler handler;  
  3.     AttachInfo attachInfo = mAttachInfo;  
  4.     if (attachInfo != null) {  
  5.         handler = attachInfo.mHandler;  
  6.     } else {  
  7.         // Assume that post will succeed later  
  8.         ViewRootImpl.getRunQueue().post(action);  
  9.         return true;  
  10.     }  
  11.   
  12.     return handler.post(action);  
  13. }  

在post() 函数注释中,明确写着:This method can be invoked from outside of the UI thread only when this View is attached to a window.

当ProgressDialog还没有attach到当前window时(ProgressDialog.show() 方法是异步执行的),mAttachInfo 值为 null,故而执行 else语句,再看一下getRunQueue()和其post() 方法:

[java]  view plain copy
  1. static final ThreadLocal<RunQueue> sRunQueues = new ThreadLocal<RunQueue>();  
  2.   
  3. static RunQueue getRunQueue() {  
  4.      RunQueue rq = sRunQueues.get();  
  5.      if (rq != null) {  
  6.          return rq;  
  7.      }  
  8.      rq = new RunQueue();  
  9.      sRunQueues.set(rq);  
  10.      return rq;  
  11.  }  
  12.  ……  
  13.  static final class RunQueue {  
  14.      private final ArrayList<HandlerAction> mActions = new ArrayList<HandlerAction>();  
  15.   
  16.      void post(Runnable action) {  
  17.          postDelayed(action, 0);  
  18.      }  
  19.   
  20.      void postDelayed(Runnable action, long delayMillis) {  
  21.          HandlerAction handlerAction = new HandlerAction();  
  22.          handlerAction.action = action;  
  23.          handlerAction.delay = delayMillis;  
  24.   
  25.          synchronized (mActions) {  
  26.              mActions.add(handlerAction);  
  27.          }  
  28.      }  
  29.         
  30.      void executeActions(Handler handler) {  
  31.          synchronized (mActions) {  
  32.              final ArrayList<handleraction> actions = mActions;  
  33.              final int count = actions.size();  
  34.   
  35.              for (int i = 0; i < count; i++) {  
  36.                  final HandlerAction handlerAction = actions.get(i);  
  37.                  handler.postDelayed(handlerAction.action, handlerAction.delay);  
  38.              }  
  39.   
  40.              actions.clear();  
  41.          }  
  42.      }  
  43.      ……  
  44.  }  
  45. andleraction>  

这样会把ProgressBar的RefreshProgressRunnable 插入到一个静态的ThreadLocal的RunQueue队列里,针对本文开头给出的例子,刷新进度的Runnable被插入到了AsyncTask 所在线程的RunQueue里; 那么插入的Runnable什么时候得到执行呢?

调用RunQueue.executeActions()方法只有一处,即在ViewRootImpl类的如下非静态方法中

[java]  view plain copy
  1. private void performTraversals() {  
  2.        ……  
  3.         if (mLayoutRequested && !mStopped) {  
  4.             // Execute enqueued actions on every layout in case a view that was detached  
  5.             // enqueued an action after being detached  
  6.             getRunQueue().executeActions(attachInfo.mHandler);  
  7.             ……  
  8.         }  
  9.   
  10.     ……  
  11. }  
该方法是在UI线程执行的(见ViewRootImpl.handleMessage()), 故当UI线程执行到该performTraversals() 里的 getRunQueue() 时,得到的是UI线程中的RunQueue,这样AsyncTask 线程中的 RunQueue永远不会被执行到, 并且AsyncTask的是用线程池实现的,AsyncTask启动的线程会长期存在,造成如下引用关系:

AsyncTask线程 => 静态的ThreadLocal的RunQueue => Runnable => ProgressBar => Activity;

如此即使activity finish 了,确始终存在一个静态引用链引用这该activity,而 Activity一般又引用着很多资源,比如图片等,最终造成严重资源泄漏。


另外,上述问题不限与ProgressBar,凡是在非UI线程使用view.post()方法,如果view没有被attach,则均存在潜在的内存泄漏的问题! 

针对本文给出的ProgressBar例子,一个简单fix方法实在 AsyncTask的doInbackground() 开始处sleep(500) 即可。 更为精准的方式可使用如下循环测试:  

[java]  view plain copy
  1. View v = progressBar.getWindow().getDecorView();  
  2. while(v.getWindowToken() == null) {  
  3.      Thread.sleep(10);  
  4. }  

上述ProgressBar例子,并不是总能再现内存泄漏的情况的(因为异步执行的不缺定性),下面再给出一个更容易再现类似问题的例子:

[java]  view plain copy
  1. public class Main extends Activity {  
  2.   
  3.     public static int a=0;  
  4.   
  5.     @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.   
  9.         TextView tv = new TextView(this);  
  10.         tv.setText("Init State");  
  11.         setContentView(tv);  
  12.   
  13.   
  14.         if (a++> 3) {  
  15.             return ;  
  16.         }  
  17.         new AsyncTask<TextView, Void, Void>() {  
  18.   
  19.             @Override  
  20.             protected Void doInBackground(TextView... params) {  
  21.                 try {  
  22.                     TextView tv = params[0];  
  23.                     //Thread.sleep(500);  
  24.                     for (int i = 1; i <= 3; ++i) {  
  25.                         doSomeHeavyWork(tv, "AsyncTask: " + i);  
  26.                         Thread.sleep(1000);  
  27.                     }  
  28.                 } catch (Exception e) {  
  29.                 }  
  30.                 return null;  
  31.             }  
  32.   
  33.             protected void onPostExecute(Void result) {  
  34.                 recreate();  
  35.             };  
  36.   
  37.         }.execute(tv);  
  38.     }  
  39.   
  40.     void doSomeHeavyWork(final TextView tv, final String text) {  
  41.         tv.post(new Runnable() {  
  42.             @Override  
  43.             public void run() {  
  44.                 tv.setText(text);  
  45.             }  
  46.         });  
  47.     }  

现象是: TextView 很大概率不会显示 "AsyncTask: 1“ 文字; 而把 Thread.sleep(500)注释掉后,一切ok!

附上MAT内存实例分析图:


转自:

http://blog.csdn.net/acrazer/article/details/7826657


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值