Android ANR Analysis Practice (a): Beijing xxx Constructs the ANR What, causes and how to avoid the ANR

Source: Internet
Author: User
Tags high cpu usage

First, what is Beijing xxx build dsluntan.com vx:17061863513anr

ANR,(Application Not Responding) 即应用程序无响应,在android应用中,当我们的UI线程被阻塞,就会弹出如下对话框,用户可以选择继续等待或者关闭这个应用程序,这种现象我们称之为ANR.

Ii. Types of ANR

The type of ANR is broadly divided into the following three types

1, the main thread to the input event in 5 seconds did not finish processing the prerequisite for this ANR is to have an input event, if the user does not trigger any input events, even if the main thread is blocked, does not produce ANR, because Inputdispatcher did not distribute events to the application, Of course also does not detect processing timeouts and reporting ANR.

2, the main thread in the execution of Broadcastreceiver onreceive function is not completed in 10 seconds, the background process is 60 seconds the ANR system does not display a dialog box prompt, only the output log.

3, the main thread in the execution of the service's various life cycle functions within 20 seconds did not complete, the background process is 200 seconds
Also, the ANR system for this situation does not display a dialog box prompt, only the output log.

Three types of ANR only the first one will have a bullet box.

Type

Method Call

Log sample

Time Out

Input Dispatch

OnClick (), OnTouch (), OnKeydown (), OnKeyup ()

Input dispatching timed out

5

Broadcast

OnReceive ()

Timeout of Broadcast

Fg:10, BG 60

Service

Onbind (), OnCreate (), Onstartcommand (), Onunbind (), OnDestroy ()

Timeout Executing service

FG:20, BG 200

Iii. Causes of ANR production

Broadly divided into two categories:

1. Problems caused by applying their own processes, such as time-consuming operations in OnCreate, OnStart and other life cycles, UI thread blocking, hangs, dead loops, etc.

2, other processes caused, such as: IO operation caused high CPU usage, resulting in the current application process can not preempt to the CPU time slice

Subdivided words can be divided into the following situations:

Time-Consuming network access
Large amount of data read and write
Database operations
Hardware operations (such as camera)
Call the thread's join () method, Sleep () method, Wait () method, or wait for the thread lock
The number of service binders reaches the limit
Watchdog ANR occurs in System server
Service busy causing timeout not responding
Other threads hold the lock, causing the main thread to wait time out
Other thread termination or crash causes the main thread to wait ...
Iv. How to avoid ANR

Time-consuming work () such as database operations, I/O, network operations, with separate worker threads
Use handler to handle Uithread and work thread interactions
Reasonable use of Handler to handle other thread requests;
Use it wisely and follow the Android lifecycle to avoid doing too much in OnCreate () and Onresume ();
Use some architectures to form specifications to avoid memory problems such as: MVP, RxJava;
Often use tools to check memory problems, such as: MAT, TraceView, as self-bring tools;
Avoid loading large pictures caused by insufficient memory to cause ANR;
Avoid the ANR caused by memory leaks.

Android ANR Analysis Practice (a): Beijing xxx Constructs the ANR What, causes and how to avoid the ANR

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.