ANR 的原因和解決方案
1、出現 ANR 的情況
滿足下面的一種情況系統就會彈出 ANR 提示
- 輸入事件(按鍵和觸控事件) 5s 內沒被處理;
- BroadcastReceiver 的事件 (
onRecieve()
方法) 在規定時間內沒處理完 (前臺廣播為 10s,後臺廣播為 60s); - Service 前臺 20s 後臺 200s 未完成啟動;
- ContentProvider 的
publish()
在 10s 內沒進行完。
通常情況下就是主執行緒被阻塞造成的。
2、ANR 的實現原理
以輸入無響應的過程為例(基於 9.0 程式碼):
最終彈出 ANR 對話方塊的位置是與 AMS 同目錄的類 AppErrors
handleShowAnrUi()
方法。這個類用來處理程式中出現的各種錯誤,不只 ANR,強行 Crash 也在這個類中處理。
// base/core/java/com/android/server/am/AppErrors.java
void handleShowAnrUi(Message msg) {
Dialog dialogToShow = null;
synchronized (mService) {
AppNotRespondingDialog.Data data = (AppNotRespondingDialog. Data) msg.obj;
// ...
Intent intent = new Intent("android.intent.action.ANR");
if (!mService.mProcessesReady) {
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
| Intent.FLAG_RECEIVER_FOREGROUND);
}
mService. broadcastIntentLocked(null, null, intent,
null, null, 0, null, null, null, AppOpsManager.OP_NONE,
null, false, false, MY_PID, Process.SYSTEM_UID, 0 /* TODO: Verify */);
boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0;
if (mService.canShowErrorDialogs() || showBackground) {
dialogToShow = new AppNotRespondingDialog(mService, mContext, data);
proc.anrDialog = dialogToShow;
} else {
MetricsLogger.action(mContext, MetricsProto.MetricsEvent.ACTION_APP_ANR,
AppNotRespondingDialog.CANT_SHOW);
// Just kill the app if there is no dialog to be shown.
mService.killAppAtUsersRequest(proc, null);
}
}
// If we've created a crash dialog, show it without the lock held
if (dialogToShow != null) {
dialogToShow.show();
}
}
不過從發生 ANR 的地方呼叫到這裡要經過很多的類和方法。最初丟擲 ANR 是在 InputDispatcher.cpp
中。我們可以通過其中定義的常量來尋找最初觸發的位置:
// native/services/inputflinger/InputDispatcher.cpp
constexpr nsecs_t DEFAULT_INPUT_DISPATCHING_TIMEOUT = 5000 * 1000000LL; // 5 sec
從這個類觸發的位置會經過層層傳遞達到 InputManagerService
中
// base/services/core/java/com/android/server/input/InputManagerService.java
private long notifyANR(InputApplicationHandle inputApplicationHandle,
InputWindowHandle inputWindowHandle, String reason) {
return mWindowManagerCallbacks.notifyANR(
inputApplicationHandle, inputWindowHandle, reason);
}
這裡的 mWindowManagerCallbacks
就是 InputMonitor
:
// base/services/core/java/com/android/server/wm/InputMonitor.java
public long notifyANR(InputApplicationHandle inputApplicationHandle,
InputWindowHandle inputWindowHandle, String reason) {
// ... 略
if (appWindowToken != null && appWindowToken.appToken != null) {
final AppWindowContainerController controller = appWindowToken.getController();
final boolean abort = controller != null
&& controller.keyDispatchingTimedOut(reason,
(windowState != null) ? windowState.mSession.mPid : -1);
if (!abort) {
return appWindowToken.mInputDispatchingTimeoutNanos;
}
} else if (windowState != null) {
try {
// 使用 AMS 的方法
long timeout = ActivityManager.getService().inputDispatchingTimedOut(
windowState.mSession.mPid, aboveSystem, reason);
if (timeout >= 0) {
return timeout * 1000000L; // nanoseconds
}
} catch (RemoteException ex) {
}
}
return 0; // abort dispatching
}
然後回在上述方法呼叫 AMS 的 inputDispatchingTimedOut()
方法繼續處理,並最終在 inputDispatchingTimedOut()
方法中將事件傳遞給 AppErrors
// base/services/core/java/com/android/server/am/ActivityManagerService.java
public boolean inputDispatchingTimedOut(final ProcessRecord proc,
final ActivityRecord activity, final ActivityRecord parent,
final boolean aboveSystem, String reason) {
// ...
if (proc != null) {
synchronized (this) {
if (proc.debugging) {
return false;
}
if (proc.instr != null) {
Bundle info = new Bundle();
info.putString("shortMsg", "keyDispatchingTimedOut");
info.putString("longMsg", annotation);
finishInstrumentationLocked(proc, Activity.RESULT_CANCELED, info);
return true;
}
}
mHandler.post(new Runnable() {
@Override
public void run() {
// 使用 AppErrors 繼續處理
mAppErrors.appNotResponding(proc, activity, parent, aboveSystem, annotation);
}
});
}
return true;
}
當事件傳遞到了 AppErrors
之後,它會藉助 Handler 處理訊息也就呼叫了最初的那個方法並彈出對話方塊。
3、ANR 的解決辦法
上面分析了 ANR 的成因和原理,下面我們分析下如何解決 ANR.
1. 使用 adb 匯出 ANR 日誌並進行分析
發生 ANR的時候系統會記錄 ANR 的資訊並將其儲存到 /data/anr/traces.txt
檔案中(在比較新的系統中會被儲存都 /data/anr/anr_*
檔案中)。我們可以使用下面的方式來將其匯出到電腦中以便對 ANR 產生的原因進行分析:
adb root
adb shell ls /data/anr
adb pull /data/anr/<filename>
在筆者分析 ANR 的時候使用上述指令嘗試匯出 ANR 日誌的時候都出現了 Permission Denied。此時,你可以將手機 Root 之後匯出,或者嘗試修改檔案的讀寫許可權,或者在開發者模式中選擇將日誌匯出到 sdcard 之後再從 sdcard 將日誌傳送到電腦端進行檢視
2. 使用 DDMS 的 traceview 進行分析
在 AS 中開啟 DDMS,或者到 SDK 安裝目錄的 tools 目錄下面使用 monitor.bat
開啟 DDMS。
TraceView 工具的使用可以參考這篇文章:《Android 效能分析之TraceView使用(應用耗時分析)》
使用 TraceView 來通過耗時方法呼叫的資訊定位耗時操作的位置。
資料:
3. 常見的 ANR 場景
- I/O 阻塞
- 網路阻塞
- 多執行緒死鎖
- 由於響應式程式設計等導致的方法死迴圈
- 由於某個業務邏輯執行的時間太長
4. 避免 ANR 的方法
- UI執行緒儘量只做跟UI相關的工作
- 耗時的工作 (比如資料庫操作,I/O,網路操作等),採用單獨的工作執行緒處理
- 用 Handler 來處理 UI 執行緒和工作執行緒的互動