LoginSignup
14
18

More than 5 years have passed since last update.

Android のANRの調査方法

Posted at

基礎知識

  • ANRは、UI(メイン)スレッドで起きる
  • UI(メイン)スレッドは、イベントループ・スレッド。だから、実行時間の長い処理があると、他をブロックして処理できなくなってしまう。

DDMSで調べる

traces.txtで調査

Strict Modeを設定する

以下を設定すると、ダイアログも表示されなくなる?

public void onCreate() {
    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                           .detectAll()
                           .penaltyLog()
                           .penaltyDeath()
                           .build());
    super.onCreate();
}

公式ドキュメントより引用

public void onCreate() {
     if (DEVELOPER_MODE) {
         StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                 .detectDiskReads()
                 .detectDiskWrites()
                 .detectNetwork()   // or .detectAll() for all detectable problems
                 .penaltyLog()
                 .build());
         StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                 .detectLeakedSqlLiteObjects()
                 .detectLeakedClosableObjects()
                 .penaltyLog()
                 .penaltyDeath()
                 .build());
     }
     super.onCreate();
 }

本番で実行したくない場合

if (BuildConfig.DEBUG)

Strict Mode ポリシー: クラス

  • StrictMode.ThreadPolicy: 特定のスレッドに適応
  • StrictMode.VmPolicy: VMプロセス内の全てのスレッドに適応

  • 公式ドキュメント StrictMode | Android Developers

パフォーマンス分析ツール

パフォーマンス分析ツール解説記事

スレッドの状態

  • running - executing application code
  • sleeping - called Thread.sleep()
  • monitor - waiting to acquire a monitor lock
  • wait - in Object.wait()
  • native - executing native code
  • vmwait - waiting on a VM resource
  • zombie - thread is in the process of dying
  • init - thread is initializing (you shouldn't see this)
  • starting - thread is about to start (you shouldn't see this either)

参考リンク

公式ドキュメント

検索用語

Android, ANR, Application Not Responding, dialog, traces.txt, adb pull, UI thread, main thread, ADT, perfomance, analysis
アンドロイド、ダイアログ、表示、UIスレッド、メインスレッド、パフォーマンス、分析、診断、デバッグ、デバッギング、プロファイリング

14
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
18