[Android] ANR ๐Ÿ’ฃ

Jayยท2021๋…„ 1์›” 18์ผ
0

Android

๋ชฉ๋ก ๋ณด๊ธฐ
11/39
post-thumbnail

Application Not Responding

  • ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‘๋‹ตํ•˜์ง€ ์•Š๋Š”๋‹ค.
  • Main Thread(UI Thread)๊ฐ€ ์ผ์ • ์‹œ๊ฐ„ ์–ด๋–ค Task์— ์žกํ˜€์žˆ๊ฒŒ ๋œ๋‹ค๋ฉด ๋ฐœ์ƒํ•˜๊ณ  ์–ดํ”Œ์ด ๊บผ์ง€๊ฒŒ ๋œ๋‹ค.

Why โ‰๏ธ

์™œ ๋ฐœ์ƒํ• ๊นŒ?

  • Application์ด UI ์Šค๋ ˆ๋“œ์— ์–ด๋– ํ•œ I/O ๋ช…๋ น(๋นˆ๋ฒˆํ•œ ๋„คํŠธ์›Œํฌ ์—‘์„ธ์Šค)์œผ๋กœ ์ธํ•ด ๋ง‰ํž ๋•Œ
  • ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‚ฌ์šฉ์ž์˜ (ํ™”๋ฉด ํ„ฐ์น˜์™€ ๊ฐ™์€)์ธํ’‹ ์‹ ํ˜ธ๋ฅผ ์ฒ˜๋ฆฌํ•˜์ง€ ๋ชปํ–ˆ์„ ๋•Œ ANR์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.
  • ๋„ˆ๋ฌด ๋งŽ์€ ์‹œ๊ฐ„ ์ •๊ตํ•œ ๋ฉ”๋ชจ๋ฆฌ ์ž‘์—… ์ง„ํ–‰์„ ํ•  ๋•Œ.

Example

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:

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

๊ตฌ๊ธ€ ๋ฌธ์„œ์—์„œ ์ฐพ์•„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
์•ˆ๋“œ๋กœ์ด๋“œ์—์„œ, ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‘๋‹ต์€ ActivityManager์™€ WindowManger System์—์„œ ๊ด€์ฐฐ์„ ํ•œ๋‹ค. ์•ˆ๋“œ๋กœ์ด๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํŠน์ • ์กฐ๊ฑด์ด ๋ฐœ๊ฒฌ๋˜๋ฉด ANR ๋‹ค์ด์–ผ๋กœ๊ทธ๋ฅผ ๋„์šฐ๊ฒŒ ๋œ๋‹ค.

1. 5์ดˆ ์ด๋‚ด์— input event๋ฅผ ๋ฐ›์ง€ ๋ชปํ•  ๊ฒฝ์šฐ.

2. Broadcast Receiver๋ฅผ 10์ดˆ ๋™์•ˆ ๋ฐ›์ง€ ๋ชปํ•  ๊ฒฝ์šฐ.


ํ”ผํ•ด์•ผ์ง€ ANR

How?

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).

  • Main Thread์—์„œ ์‹คํ–‰๋˜๋Š” ๋ฉ”์†Œ๋“œ๋Š” ๊ฐ€๋Šฅํ•œ ์ ์€ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•œ๋‹ค.
  • onCreate() / onResume()์—์„œ ๊ฐ€๋Šฅํ•œ ์ตœ์†Œ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•œ๋‹ค.
  • Network Access, Db operation, bitmap resizing ๊ฐ™์€ ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋Š” ์ž‘์—…์€ ๋ฉ”์ธ ์Šค๋ ˆ๋“œ๋ฅผ ์ง€์–‘ํ•˜์ž.

UI ๋ณ€๋™์ด ์žˆ์„ ๋•Œ๋Š” ํ”„๋กœ๊ทธ๋ž˜์Šค ๋ฐ”์™€ ๊ฐ™์€ ํ™”๋ฉด์œผ๋กœ ์‚ฌ์šฉ์ž์—๊ฒŒ ์•Œ๋ ค์ฃผ์ž.


๐Ÿคซ

์•„์˜ˆ ๋ฐœ์ƒํ•˜์ง€ ์•Š์„ ์ˆ˜ ์—†๋‹ค. = ๋ฐœ์ƒํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋‹ค.
๊ธฐ๊ธฐ์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ธฐ์— ๊ฐœ๋ฐœ์ž์˜ ์—ญ๋Ÿ‰์€ ์ด๋ฅผ ์ตœ์†Œํ™” ํ•˜๋Š” ๊ฑฐ๋ผ๊ณ  ์ƒ๊ฐํ•œ๋‹ค.
(๋‚˜๋„ ๊ฐ€๋” ์–ดํ”Œ์„ ์“ฐ๋‹ค๋ณด๋ฉด ํˆญํ•˜๊ณ  ๊บผ์งˆ๋•Œ๊ฐ€ ์žˆ๋‹ค.)
์‚ฌ์šฉ์ž์—๊ฒŒ ์ด๋Ÿฌํ•œ ๊ฒฝํ—˜์„ ์ตœ์†Œํ™” ์‹œํ‚ค๋Š”๊ฒŒ ๊ฐœ๋ฐœ์ž์˜ ์ผ์ด๋ผ๊ณ  ์ƒ๊ฐํ•œ๋‹ค.

profile
Android Developer - Come to my medium (https://medium.com/@wodbs135)

0๊ฐœ์˜ ๋Œ“๊ธ€