如何分析Android ANR問題?
ANR(Application Not Responding)是指應用程式無法回應用戶操作,通常是因為主線程被阻塞,造成應用程式失去了響應能力。在 Android 開發中,ANR 通常是由長時間的 IO 操作、低效的網路訪問、死循環、死鎖等問題所引起的。
以下是一些分析 Android ANR 問題的方法:
查看 ANR 日誌:當應用程式出現 ANR 問題時,系統會生成一個 ANR 日誌,其中包含了造成 ANR 的原因、發生時間、應用程式名稱等資訊。開發人員可以查看 ANR 日誌來了解問題的根本原因。
使用 systrace 工具:systrace 是 Android SDK 中一個用於分析系統效能的工具,可以用來分析 CPU、GPU、網路等方面的效能問題。開發人員可以使用 systrace 工具來檢查應用程式是否因為佔用過多的資源而導致 ANR。
使用 Profiler 工具:Profiler 是 Android Studio 中的一個效能分析工具,可以用來分析應用程式的 CPU、記憶體、網路等方面的效能問題。開發人員可以使用 Profiler 工具來檢查應用程式的效能瓶頸,並優化應用程式的效能。
檢查應用程式代碼:開發人員可以檢查應用程式代碼,尋找可能造成 ANR 的問題。例如,長時間的 IO 操作應該在後台執行,不應該在主線程中執行。低效的網路訪問應該使用異步方式執行,避免阻塞主線程。
測試應用程式:開發人員可以進行一系列的測試,測試應用程式在各種情況下的反應速度和效能。例如,模擬網路緩慢的情況,測試應用程式的反應速度。這樣可以幫助開發人員發現可能會導致 ANR 的問題,並進行修復。
總之,要分析 Android ANR 的問題,需要進行綜合分析
===
2012: How to trace ANR problem !
What Triggers ANR?
http://developer.android.com/guide/practices/design/responsiveness.html
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 (e.g. key press, screen touch) within 5 seconds
A BroadcastReceiver hasn't finished executing within 10 seconds
How to investigate an ANR http://stackoverflow.com/questions/704311/android-how-do-i-investigate-an-anr
An ANR happens when some long operation takes place in the "main" thread. This is the event loop thread, and if it is busy, Android cannot process any further GUI events in the application, and thus throws up an ANR dialog.
Now, in the trace you posted, the main thread seems to be doing fine, there is no problem. It is idling in the MessageQueue, waiting for another message to come in. In your case the ANR was likely a longer operation, rather than something that blocked the thread permanently, so the event thread recovered after the operation finished, and your trace went through after the ANR.
Detecting where ANRs happen is easy if it is a permanent block (deadlock acquiring some locks for instance), but harder if it's just a temporary delay. First, go over your code and look for vunerable spots and long running operations. Examples may include using sockets, locks, thread sleeps, and other blocking operations from within the event thread. You should make sure these all happen in separate threads. If nothing seems the problem, use DDMS and enable the thread view. This shows all the threads in your application similar to the trace you have. Reproduce the ANR, and refresh the main thread at the same time. That should show you precisely whats going on at the time of the ANR
--
中文相關討論
留言
張貼留言
歡迎留言一起討論