✅ 分析流程清单
- 1. 查看ANR Subject,确定ANR类型和触发位置
- 2. 找到主线程堆栈(搜索应用包名或"main")
- 3. 从堆栈顶部(#00)分析系统层调用特征
- 4. 从堆栈底部往上追溯应用层调用链
- 5. 识别关键阻塞点(Lock、IO、Binder等)
- 6. 搜索相关线程,分析是否有线程竞争
- 7. 推测根本原因,制定解决方案
- 8. 验证修复效果
常见ANR类型识别
根据堆栈特征判断:
| 堆栈特征 | ANR类型 |
|---|---|
Monitor::Lock / monitor_enter | 死锁/锁竞争 |
epoll_wait / MessageQueue.next | 主线程空闲,等待消息 |
| IO操作(read/write) | IO阻塞 |
| GC相关调用 | GC频繁 |
| Binder通信超时 | IPC问题 |
🔍 关键堆栈特征速查
死锁/锁竞争:
Monitor::Lock, ExclusiveLock, monitor_enter, synchronized
主线程等待(正常):
epoll_wait, MessageQueue.next, Looper.loop
IO阻塞:
read, write, __ioctl, recvfrom, sendto
Binder超时:
IPCThreadState::waitForResponse, executeCommand
GC问题:
WaitForGcToComplete, CollectGarbage
网络请求:
connect, Socket, HttpURLConnection
📚 常用命令
# 搜索主线程
grep -A 100 "\"main\"" anr_file
# 搜索应用包名
grep -n "your.package.name" anr_file
# 查看所有线程状态
grep "sysTid=" anr_file
# 搜索特定方法
grep -C 10 "YourClass.method" anr_file
# 统计线程状态
grep "sysTid=" anr_file | awk '{print $NF}' | sort | uniq -c
附录:ANR分析工具推荐
- Android Studio Profiler: 实时监控线程和锁
- StrictMode: 开发期检测主线程违规
- Systrace: 系统级性能分析
- adb命令:
adb shell dumpsys activity anrs - 第三方工具: DDMS、MAT(内存分析)