Android查看应用线程信息

/ 0评 / 0

通过adb命令

获取pid

ps -A | grep 应用包名

获取进程信息

adb shell  cat /proc/[获取pid]/status

file

当然也可以使用ps命令查看

ps -T -p [pid]

file

top自动更新线程信息

top -H -p [pid]

file

通过代码获取

object ThreadUtils {

    fun getAllThreadSize(): Int {
        var size = 0
        File("/proc/self/status").reader().forEachLine { line ->
            if (line.startsWith("Threads")) {
                //Threads:  1
                val regex = "\\d+".toRegex()
                val matchResult = regex.find(line)
                size = matchResult?.value?.toInt() ?: 0
            }
        }
        return size
    }

    fun getAllJavaThreads(): List<Thread> {
        var system = Thread.currentThread().threadGroup
        while (system?.parent != null) {
            system = system.parent
        }
        val count = system?.activeCount() ?: 0
        //see java.lang.Thread.getAllStackTraces
        val threads = arrayOfNulls<Thread>(count + count / 2)
        system?.enumerate(threads)
        val list = mutableListOf<Thread>()
        threads.forEach {
            it?.let { it1 -> list.add(it1) }
        }
        return list
    }
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注