如何分析Android ANR問題? How to trace ANR



ANR(Application Not Responding)是指 Android 系統中當應用程式沒有及時響應用戶操作而被強制關閉的情況。這種情況通常是由於某些長時間運行或阻塞 UI 线程的操作引起的。以下是一些分析 Android ANR 問題的方法:

  • 使用 DDMS 工具:DDMS 可以用來監視正在運行的 Android 應用程式,包括線程和記憶體使用情況。可以使用 DDMS 工具來檢查是否有某個線程正在阻塞 UI 线程,或者是否有任何內存泄漏等問題。
  • 查看 ANR 日誌:當系統發生 ANR 時,會生成一個 ANR 日誌文件。可以使用 Android Studio 或者其他文本編輯器來查看這個日誌文件,以了解發生 ANR 的原因和具體信息。通常,這個日誌文件會包含 ANR 發生的時間、應用程式的 PID、當前線程的狀態和堆棧跟踪信息等等。
  • 分析 ANR 堆棧:當應用程式發生 ANR 時,堆棧跟踪信息中會顯示導致 ANR 的原因。開發人員可以分析這個堆棧跟踪信息,以找到造成問題的代碼或者操作。
  • 記憶體回收機制:如果應用程式使用大量記憶體,就有可能導致 ANR。開發人員可以使用 Android SDK 提供的記憶體回收機制,例如手動執行 GC(Garbage Collection)或使用記憶體快取等方法,以減少內存使用量,提高應用程式性能。

總之,分析 Android ANR 問題需要仔細查看記憶體使用情況、應用程式運行狀態和相關日誌信息,以找到問題所在,並進行相應的優化和調整。


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

中文相關討論

留言

Google Search

推薦內容橫式

本月熱門文章

鋼鐵韓粉站出來 讓韓國瑜每天唱歌喝酒好不好

黑金? 不得不提前總統馬英九大姊馬以南 吳敦義 林益世

香港事件回顧 2020/08/10 前眾志成員周庭及壹傳媒創辦人黎智英被捕 今日累計10人被捕

從台灣省長宋楚瑜到台北市長柯文哲,可以說 宋楚瑜 2.0 就是柯文哲

捐款支持義大利靈醫會 一起來幫助他們 就像當初教士來台灣協助我們一樣 !!

「港版國安法」,法律將會放在《基本法》附件三在港實施,而非就《基本法》23條立法

除了兒福聯盟 還有其他團體是努力守護孩子們 芥菜種會愛心育幼院 及忠義育幼院 都需要大家幫忙

兒福聯盟到底多有錢?收捐款為什麼不做事情而是定存?王育敏不解釋嗎?

北京故宮首訪,一窺清宮秘史 大玉兒 & 甄嬛

這個網誌中的熱門文章

Android應用開發豆知識:利用 adb 安裝 apk 到裝置上

Android 中文輸入法 官方版 ! Gboard - Google 鍵盤 開始支援注音輸入啦

Google Play 推薦Android app 誠徵App排行榜

北京故宮首訪,一窺清宮秘史 大玉兒 & 甄嬛

[家教][社會觀察] 建中教我的事 沒上建中被父母親折磨? 在建中到底是如何 ...

[FAANG面試] Amazon/AWS 領導力準則 14 Amazon Leadership Principles

Acer ICONIA Smart S300 更新後越來越好 Acer也有出手機?!

[品質控制] 什麼是Sanity test ? 軟體測試常見名詞整理 包含不同部門的測試人員負責範圍

新鮮人找工作:職場名詞解釋 AE FAE Pre-sales Post-sales