Android DalvikVM threads and ANR dump.

本文详细解释了Android Dalvik虚拟机中的线程状态及其转换,包括RUNNING与NATIVE状态的特点,以及SUSPENDED状态如何用于垃圾回收和调试过程中。特别强调了当应用程序发生ANR时,仅Java堆栈会被转储。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://infinitesproutingthoughts.wordpress.com/2013/06/21/deciphering-android-garbage-collection-gc-logs/

Android dalvikVM (DVM) provides own Thread wrapper on top of native threads(Linux posix threads).

Below Excerpts are explained and understood from Android   Source code location “dalvik/vm/Thread.h/.cpp”

A Java thread can be any of below status:

  1. UNDEFINED
  2. ZOMBIE : thread got terminated.
  3. RUNNING: 
  4. TIMED_WAIT:
  5. WAIT
  6. INITIALIZING
  7. STARTING
  8. NATIVE
  9. VMWAIT
  10. SUSPENDED

RUNNING and NATIVE:

Two thread states  are tricky to understand  RUNNING  and NATIVE, RUNNING corresponds when thread is running in dalvikvm(Dvm) and NATIVE when same thread enters in JNI native methods. As per Dalvik JNI implementation(dalvik/vm/Jni.cpp)  ”All JNI methods must start by changing their thread status to THREAD_RUNNING, and finish by changing it back to THREAD_NATIVE before returning to native code”, i.e whenever a native call invokes a JNI method, the Thread Status is set to RUNNING and when it comes back to native it is reset to NATIVE, and this achieved by  C++ class “ScopedJniThreadState” which calls dvmChangeStatus(mSelf, THREAD_RUNNING); in it’s constructor and dvmChangeStatus(mSelf, THREAD_NATIVE); in Destructor

SUSPENDED and ANR stack dump.

A java thread can be placed in SUSPENDED state  mostly due to GC(CONCURRENT) collection and debugger(DDMS), when any of these operations are performed all thread except debugger thread(JDWP) and SignalCacther are suspended,

In case of DVM Singal catcher(A per process global signal catcher) when received any signal like SIGQUIT (3) sent from ActivityManagerService due to ANR/slow operation, it calls dvmSuspendAllThreads(), which suspend all java threads(not native) to make them wait on gDvm.threadSuspendCountCond, once all threads stack is dumped , it resumes all the suspended thread by broadcasting on waited condition.

A Catch:  Java thread in NATIVE  state doesn’t get affected by above suspended operation hence on ANR dump only Java Stack trace gets dumped except binder threads. So on ANR dump, if main thread sate = NATIVE,  main thread was executing some native method, and  if = SUSPENDED, it was in java method and got suspended to dump stack traces.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值