Handler形成內存泄露的緣由。非靜態內部類,或者匿名內部類。使得Handler默認持有外部類的引用。在Activity銷燬時,因爲Handler可能有未執行完/正在執行的Message。致使Handler持有Activity的引用。進而致使GC沒法回收Activity。bash
//匿名內部類
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
複製代碼
//非靜態內部類
protected class AppHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
// TODO: 2019/4/30
}
}
}
複製代碼
// 清空消息隊列,移除對外部類的引用
@Override
protected void onDestroy() {
super.onDestroy();
mHandler.removeCallbacksAndMessages(null);
}
//Handler源碼中removeCallbacksAndMessages()註釋含義
/**
* Remove any pending posts of callbacks and sent messages whose
* <var>obj</var> is <var>token</var>. If <var>token</var> is null,
* all callbacks and messages will be removed.
*/
public final void removeCallbacksAndMessages(Object token) {
mQueue.removeCallbacksAndMessages(this, token);
}
複製代碼
private static class AppHandler extends Handler {
//弱引用,在垃圾回收時,被回收
WeakReference<Activity> activity;
AppHandler(Activity activity){
this.activity=new WeakReference<Activity>(activity);
}
public void handleMessage(Message message){
switch (message.what){
//todo
}
}
}
複製代碼
即便內存泄漏了。待handler中的消息處理完。下次GC時便可回收本次未回收的內存。ide