Crash optimization summary

Java layer crashes

The cause of the crash

  • When an exception is not caught in the code, the JVM will call Thread.dispatchUncaughtExceptionthe method for us

  • In this method, it will be judged whether there is a setting UncaughtExceptionHandler; if there is, let it call uncaughtExceptionthe method to handle the exception; if not, use the default exception handler for processing

  • When the app process is just created, (RuntimeInit.java) will set a default exception handler for the thread KillApplicationhandler, which will report the exception information to AMS, and then kill the app process

Solution

  • Set a global exception handler for Thread, collect crash information in it and report it to the server

  • A normal crash will kill the entire app process. After catching the exception, only the currently displayed Activity can be closed to improve user experience

  • Generally, the crash occurs when the main thread Looper processes messages. We can add a layer outside the Looper loop to process messages try/catch, so as to prevent the application from being killed;

    • The specific method is to send a message in the main thread Looper through the Handler, open an infinite loop in this message, call it in the loop Looper.loop()and try/catchwrap it with wrapping, when an exception occurs, it will be caught, and then open a loop to traverse the message again, so as not to crash Purpose

    • Handler(Looper.getMainLooper()).post {
              
              
          while (true) {
              
              
              try {
              
              
                  Looper.loop()//开启新的循环
              } catch (e: Throwable) {
              
              
                  e.printStackTrace()
              }
          }
      }
      

Native layer crashes

System native crash monitoring

  • When SystemServer starts the system service, it will create a NativeCrash monitor through AMS. By monitoring the crash semaphore of the native layer, it is judged that the crash information will be saved to the directory through DropBoxManager after the crash /data/system/dropbox.

Native layer crash monitoring in the application

  • Listen to the semaphore related to crash in the native layer through JNI, and then call back the java layer method to record and save it

  • ndk provides the addr2line tool to locate the number of lines of code according to the memory address

  • Third-party frameworks: breakpad, Tencent Bugly

ANR

Types of ANRs

Input event and Service\broadcast, ContentProvider did not finish processing the event within the specified time

  • "Input event dispatching timed out" => No response to touch event within 5 seconds

  • "Time out executing service" = "Serveice is not started within the specified time, the foreground service is 20 seconds, and the background is 200 seconds

  • "Time out of broadcast BroadcastRecord" => The broadcast is not processed within the specified time, the foreground broadcast is 10 seconds, and the background broadcast is 60 seconds

  • "time out publish content provider" => ContentProvider did not publish within 10 seconds

The cause of ANR

  • When the system detects ANR, it mainly sends a delayed message through Handler before processing the event. If it is processed within the specified time, the message will be removed. Otherwise, the ANR operation will be performed, and the ANR information will be saved and an ANR window prompt will pop up; the ANR information is saved in in /data/anr/traces.txtthe file;

  • In the application layer, it is mainly caused by the main thread doing time-consuming operations, or frequently triggering GC, deadlock and other problems.

ANR problem analysis

  • View information such as the time when anr occurs, process id, package name, ANR type, etc. through the system log

  • Analyze the CPU usage when ANR occurs. If the app usage is high, it is considered to be an app problem. If the system cpu usage is high, it may be caused by the system.

  • Then analyze the trace file of ANR, mainly looking at the state of the main main thread, and the printed stack information to locate the location where ANR occurred; for example, whether the main thread was waiting for other locks to be released, or processing sleep, wait and other states, etc.

ANR monitoring

  • By FileObservermonitoring data/anr/the file changes in the directory, when ANR occurs, a trace file will be generated in the directory; Tencent Bugly is this way of implementation

  • Open a separate thread and modify a variable on the main thread through the Handler. If the modification is not successful within the set time, it is considered that ANR has occurred, and then the main thread call stack information is obtained; the implementation of ANR-WatchDog

  • Realization of semaphore monitoring when ANR occurs in the native layer; WeChat implementation method

other crashes

  • wtf (What a Terrible Failure): Generally, it is an exception thrown by the official Android code that is not installed, such as sending an unprotected broadcast, not adding a tag when the background service starts the Activity, Intent.FLAG_ACTIVITY_NEW_TASKetc.

Guess you like

Origin blog.csdn.net/guangdeshishe/article/details/129903028