向IntentService发送消息时产生了多个匿名线程

时间:2018-08-29 21:25:31

标签: android multithreading kotlin intentservice android-intentservice

我刚接触Android开发,可能这个问题太简单了,以至于我找不到关于如何调试或解决问题的明确解释。

在我的应用中,我有一个 SensorDataListener 类,该类由主活动实例化并保留了它的引用,并且在 onSensorChanged中 回调(以及其他一些事件)在名为 SensorDataCacheService 的IntentService中启动作业。 现在,虽然该机制似乎正常工作,但是每当一条消息添加到IntentService的队列时,就会产生3到5个线程(名为Thread-1,Thread-2等),并永远保持空闲状态,从而迅速填充了内存,直到应用程序爆炸。

我可以在Android Studio的Android Profiler中看到这种情况,但是我尽一切努力找出是什么原因启动了它们:即使 onHandleIntent 的内容已完全注释,我很确定这一定是我所做的某种基本错误的设置。

在这里您可以看到: SensorDataListener

class SensorDataListener(owner: TrainingActivity) : SensorEventListener {
    private var context: TrainingActivity = owner
    private var settings = PreferenceManager.getDefaultSharedPreferences(context) as SharedPreferences
    private var sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager
    var isRecording = false

    companion object {
        private val TAG = "SensorDataListener"
    }

    // SensorEventListener overrides
    override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
        // No need for this at the moment
    }

    override fun onSensorChanged(event: SensorEvent?) {
        if (event != null) {
            var intent = Intent(context, SensorDataCacheService::class.java)
            intent.putExtra(SENSOR_EVENT_ACCURACY.name, event.accuracy)
            intent.putExtra(SENSOR_EVENT_SENSOR_TYPE.name, event.sensor.stringType)
            intent.putExtra(SENSOR_EVENT_TIMESTAMP.name, event.timestamp)
            intent.putExtra(SENSOR_EVENT_VALUES.name, event.values)
            intent.putExtra(MESSAGE_TYPE.name, ADD_TO_CACHE)
            context.startService(intent)
        }
    }

    ...
}

SensorDataCacheService

class SensorDataCacheService : IntentService("SensorDataCacheService") {
    private lateinit var db: CouchdbClient

    companion object {
        private const val TAG = "SensorDataCacheService"
        private var cache = mutableMapOf<String, MutableList<FeatureData>>()
    }

    override fun onCreate() {
        super.onCreate()
        db = CouchdbClient(this)
    }

    override fun onHandleIntent(intent: Intent?) {
        when (intent?.extras?.get(MESSAGE_TYPE.name)) {
            ADD_TO_CACHE -> {
                cache(intent)
                Log.i(TAG, "Add to cache, on ${Thread.currentThread().name}")
            }
            CLEAR_CACHE -> {
                clearCache()
            }
        }
    }

    ...
}

有趣的是,即使我确定 onHandleIntent 方法的内容实际上是由IntentService的工作线程执行的(我正在从内部打印线程的名称),每次调用的缓存似乎都不一样,因此我必须将其设为静态(kotlin中的随播对象),以使其在所有调用之间共享。

崩溃之后,logcat(详细级别)中显示的唯一内容是:

08-30 20:21:14.587 31786-9669/com.mvettosi.touchlogger A/osi.touchlogge: thread.cc:3888] Unable to create protected region in stack for implicit overflow check. Reason: Out of memory size:  4096
08-30 20:21:15.747 31786-9669/com.mvettosi.touchlogger A/osi.touchlogge: runtime.cc:558] Runtime aborting...
    runtime.cc:558] Dumping all threads without appropriate locks held: thread list lock mutator lock
    runtime.cc:558] All threads:
    runtime.cc:558] DALVIK THREADS (7053):
    runtime.cc:558] "osi.touchlogger" prio=10 (not attached)
    runtime.cc:558]   | sysTid=9669 nice=-10 cgrp=default
    runtime.cc:558]   | state=R schedstat=( 771357331 2945888 18 ) utm=72 stm=3 core=7 HZ=100
    runtime.cc:558]   native: #00 pc 00000000003c7324  /system/lib64/libart.so (art::DumpNativeStack(std::__1::basic_ostream<char, std::__1::char_traits<char>>&, int, BacktraceMap*, char const*, art::ArtMethod*, void*, bool)+220)
    runtime.cc:558]   native: #01 pc 00000000004a86c4  /system/lib64/libart.so (art::ThreadList::DumpUnattachedThreads(std::__1::basic_ostream<char, std::__1::char_traits<char>>&, bool)+340)
    runtime.cc:558]   native: #02 pc 00000000004a7c6c  /system/lib64/libart.so (art::ThreadList::Dump(std::__1::basic_ostream<char, std::__1::char_traits<char>>&, bool)+460)
    runtime.cc:558]   native: #03 pc 000000000046a9d0  /system/lib64/libart.so (art::Runtime::Abort(char const*)+392)
    runtime.cc:558]   native: #04 pc 0000000000008d2c  /system/lib64/libbase.so (android::base::LogMessage::~LogMessage()+724)
    runtime.cc:558]   native: #05 pc 00000000004905b8  /system/lib64/libart.so (art::Thread::ProtectStack(bool)+400)
    runtime.cc:558]   native: #06 pc 00000000004901cc  /system/lib64/libart.so (art::Thread::InstallImplicitProtection()+132)
    runtime.cc:558]   native: #07 pc 0000000000491bd8  /system/lib64/libart.so (art::Thread::InitStackHwm()+504)
    runtime.cc:558]   native: #08 pc 000000000048fe40  /system/lib64/libart.so (art::Thread::Init(art::ThreadList*, art::JavaVMExt*, art::JNIEnvExt*)+184)
    runtime.cc:558]   native: #09 pc 000000000048f400  /system/lib64/libart.so (art::Thread::CreateCallback(void*)+112)
    runtime.cc:558]   native: #10 pc 0000000000083114  /system/lib64/libc.so (__pthread_start(void*)+36)
    runtime.cc:558]   native: #11 pc 00000000000233bc  /system/lib64/libc.so (__start_thread+68)
    runtime.cc:558] 
    runtime.cc:558] (Aborting thread was not attached to runtime!)
    runtime.cc:558]   native: #00 pc 00000000003c7324  /system/lib64/libart.so (art::DumpNativeStack(std::__1::basic_ostream<char, std::__1::char_traits<char>>&, int, BacktraceMap*, char const*, art::ArtMethod*, void*, bool)+220)
    runtime.cc:558]   native: #01 pc 000000000046a980  /system/lib64/libart.so (art::Runtime::Abort(char const*)+312)
    runtime.cc:558]   native: #02 pc 0000000000008d2c  /system/lib64/libbase.so (android::base::LogMessage::~LogMessage()+724)
    runtime.cc:558]   native: #03 pc 00000000004905b8  /system/lib64/libart.so (art::Thread::ProtectStack(bool)+400)
    runtime.cc:558]   native: #04 pc 00000000004901cc  /system/lib64/libart.so (art::Thread::InstallImplicitProtection()+132)
    runtime.cc:558]   native: #05 pc 0000000000491bd8  /system/lib64/libart.so (art::Thread::InitStackHwm()+504)
    runtime.cc:558]   native: #06 pc 000000000048fe40  /system/lib64/libart.so (art::Thread::Init(art::ThreadList*, art::JavaVMExt*, art::JNIEnvExt*)+184)
    runtime.cc:558]   native: #07 pc 000000000048f400  /system/lib64/libart.so (art::Thread::CreateCallback(void*)+112)
    runtime.cc:558]   native: #08 pc 0000000000083114  /system/lib64/libc.so (__pthread_start(void*)+36)
    runtime.cc:558]   native: #09 pc 00000000000233bc  /system/lib64/libc.so (__start_thread+68)
    runtime.cc:558] 
08-30 20:21:15.754 31786-9669/com.mvettosi.touchlogger A/libc: Fatal signal 6 (SIGABRT), code -6 (SI_TKILL) in tid 9669 (osi.touchlogger), pid 31786 (osi.touchlogger)

你们中的任何人对发生这种情况的原因有任何想法,或者我可以采用哪种方法找出正在发生的情况?

预先感谢

0 个答案:

没有答案