정상에서 IT를 외치다

[Android, ANR] Android Not Responding 는 무엇인가? 본문

안드로이드

[Android, ANR] Android Not Responding 는 무엇인가?

Black-Jin 2019. 3. 23. 10:40
반응형


안녕하세요. 블랙진입니다.

ANR에 대해 글문서를 기본으로 내용을 정리해 보겠습니다.


Application Not Responding (ANR)


어느 동작에서 메인 스레드(UI 스레드)가 오랫동안 점유하고 있으면 발생하는 메시지 입니다. 



언제 발생하는가?


구글 문서에서는 다음과 같이 언급하고 있습니다.


Generally, the system displays an ANR if an application cannot respond to user input.

어플리케이션이 사용자의 (화면 터치와 같은)인풋 신호를 처리하지 못했을 때 ANR 다얼로그를 보여줍니다. 


an application blocks on some I/O operation (frequently a network access) on the UI thread so the system can't process incoming user input events.

메인 스레드 에서 (흔히 네트워크 접근과 같은) 작업 중일 때 인풋 동작이 막힐 때


the app spends too much time building an elaborate in-memory structure 

너무 많은 시간을 정교한 메모리 작업에 사용할 때 



ANR 발생 예시


구글 문서에서는 두가지 구체적인 상황을 제시해 줍니다.


In Android, application responsiveness is monitored by the Activity Manager and Window Manager system services. Android will display the ANR dialog for a particular application when it detects one of the following conditions:

  • No response to an input event (such as key press or screen touch events) within 5 seconds.
  • BroadcastReceiver hasn't finished executing within 10 seconds.


안드로이드에서는 ActivityManager 와 Window Manager System 에서 어플리케이션 응답을 관찰합니다. 그러다 아래와 같은 상황이 오면 ANR 다이얼 로그를 사용자에게 보여줍니다.


 - 5초 이내에 input event 를 받지 못했을 때


 - 브로드캐스트 리시버를 10초동안 못 받았을 때



ANR을 어떻게 피하는가?


any method that runs in the UI thread should do as little work as possible on that thread. In particular, activities should do as little as possible to set up in key life-cycle methods such as onCreate() and onResume(). Potentially long running operations such as network or database operations, or computationally expensive calculations such as resizing bitmaps should be done in a worker thread (or in the case of databases operations, via an asynchronous request).


메인스레드에서 실행되는 메소드는 가능한 한 적은 작업을 수행해야 합니다. 특히 엑티비티의 onCreate() 그리고 onResume() 에서는 가능한한 최소한의 작업만을 수행해야 합니다. 네트워크 접근, 데이터 처리 또는 비트맵 리사이징과 같은 잠재적으로 오래 걸리는 작업은 작업스레드에서 처리해야 합니다.


If your application is doing work in the background in response to user input, show that progress is being made (such as with a ProgressBar in your UI)


 UI 변동이 있을 때는 프로그레스와 같은 화면을 띄어 사용자에게 기달릴 것을 명시해 줍니다.



정리


아무리 앱을 잘 만들어도 단말 상태가 좋지 않으면 ANR이 발생 할 수 밖에 없습니다. 개발자는 이를 최대한 줄이는 것이 그나마 가능한 목표이고 구글에서도 이를 위한 가이드를 제시해 주고 있습니다(문서를 보시면 코드와 함께 더욱 자세한 예시가 있습니다) 개발자는 항상 이러한 에러 발생을 줄여 안정적이고 사용자 경험을 해치지 않는 앱을 개발하는게 노력을 해야겠습니다.

반응형
Comments