ANR questions frequently asked in Android interviews

1. ANR definition

The main thread of the Android application is blocked for too long, triggering an "Application not Responding" (Anr) error. If the application is in the foreground, there will be a pop-up window.

2. Reasons for ANR

1. Time-consuming operation of the main thread
2. The main thread is synchronously locked by the child thread
3. The main thread is blocked by the binder peer
4. The binder thread is full
5. Cannot get system resources

3. Several types of ANR

1. Service TimeOut timeout
Timeout reason:
timeout when executing service lifecycle methods: oncreate, onstart onbind Timeout
time: 20s for foreground service, 200s for background service
2.BroadCast TimeOut broadcast timeout timeout
reason:
at the specified time, the onReceive method is executed Timeout
Timeout time: 10s in the foreground, 60s in the background
3. InputDispatching Timeout
Input event timeout, such as click, no response to touch event
Timeout time: 5s

ServiceTimeout trigger mechanism

bumpServiceExecutingLocked(); Buried a bomb, if the corresponding life cycle is not executed within the corresponding time, it will anr, if it is processed, it will
send removemsg

 void scheduleServiceTimeoutLocked(ProcessRecord proc) {
        if (proc.executingServices.size() == 0 || proc.thread == null) {
            return;
        }
        Message msg = mAm.mHandler.obtainMessage(
                ActivityManagerService.SERVICE_TIMEOUT_MSG);
        msg.obj = proc;
        mAm.mHandler.sendMessageDelayed(msg,
                proc.execServicesFg ? SERVICE_TIMEOUT : SERVICE_BACKGROUND_TIMEOUT);
    }

Handle SERVICE_TIMEOUT_MSG messages

com.android.server.am.ActivityManagerService#SERVICE_TIMEOUT_MSG
  case SERVICE_TIMEOUT_MSG: {
                mServices.serviceTimeout((ProcessRecord)msg.obj);
            } break;
//日志里的第一现场:Timeout executing service
Slog.w(TAG, "Timeout executing service: " + timeout);
StringWriter sw = new StringWriter();
PrintWriter pw = new FastPrintWriter(sw, false, 1024);
pw.println(timeout);
timeout.dump(pw, "    ");
pw.close();
mLastAnrDump = sw.toString();
mAm.mHandler.removeCallbacks(mLastAnrDumpClearer);
mAm.mHandler.postDelayed(mLastAnrDumpClearer, LAST_ANR_LIFETIME_DURATION_MSECS);
 anrMessage = "executing service " + timeout.shortInstanceName;

BroadCast Timeout trigger mechanism:

Broadcasts are divided into: serial, parallel
broadcasts that cause timeouts are serial broadcasts:
orderly broadcasts with dynamic registration
static registration broadcasts

2. ANR positioning process

1. View ANR in through logcat to view cpu information and system load status,
apk package name, process number, the reason for triggering ANR, the cpu usage of each application in the system during the time period when ANR occurs,
such as input timeout, and ANR is based on the timeout time. Push forward for 5s, use application is not responding to filter, and filter monkey logs

2. Find the log of anr, and check the trace log information
/data/anr of the main thread main
3. Locate the specific time period when ANR occurred
4. Check the state of the system during the time period when ANR occurred

3. The way to avoid ANR: reduce the time-consuming operation of the main thread

1.ServiceTimeout
avoids time-consuming application oncreate
and avoids time-consuming service life cycle. You
can use IntentService
2.BroadCastTimeout
can use IntentService
3.InputDispatching Timeout
avoids time-consuming main thread

4. Reasonable performance optimization can also avoid

1. Avoid new objects in while or ondraw
2. Use the object pool Android.util.Pools
3. Check redundant background images, especially the background images of the parent layout
4. Reduce layout levels: constrainlayout, merge
5. Delay loading layout viewstub

4. ANR detection tool

blockcanary
strictmode
leakcanary

Guess you like

Origin blog.csdn.net/github_37610197/article/details/125109776