当前位置:   article > 正文

Android性能监测_adb top

adb top

1.使用adb top可以查看当前Android设备的CPU和内存(mem)使用情况

adb shell top
  • 1

打印的信息如下:
在这里插入图片描述
PID 表示进程号
USER 表示进程所属用户组
PR 进程的优先级,值越小,优先级越高
NI 进程的nice值,决定了CPU调度优先级,值越小,优先级越高
VIRT 进程使用的虚拟内存大小
RES 进程使用的实际物理内存大小
SHR 被多个进程共享的内存大小
S 进程的状态,包括R(运行态)、S(睡眠态)、Z(僵尸态)等
%CPU 进程当前的CPU占用率,很可能超过100%,存在多核的情况
%MEM 进程当前的内存占用率
TIME+ 进程自启动以来运行的时间
ARGS 进程的包名,或者说叫进程名

2.使用adb top还可以过滤查看单个进程的CPU和内存(mem)使用情况

adb shell top -d 1 | grep com.google.navi(包名)
  • 1

这里会每1秒中打印一次具体包名对应的进程情况,如下:
在这里插入图片描述
3.可以通过dumpsys meminfo来查看当前特定进程的内存情况

adb shell dumpsys meminfo com.google.naviauto(包名)
  • 1

可以获取到如下的信息:
在这里插入图片描述
Native Heap: 这部分内存用于存储C/C++代码中的原生对象,如malloc和free分配的内存。

Dalvik Heap: 这部分内存用于存储Java虚拟机(Dalvik或ART)中的Java对象。

Views: 这是UI组件(如视图和布局)占用的内存。

App: 这是应用程序本身的内存占用,包括代码、数据和资源。

Private Dirty: 这是应用程序的私有内存,无法被其他应用程序共享。

Private Clean: 这是应用程序的私有内存,但它是干净的,可以与其他应用程序共享。

** Pss: Proportional Set Size(PSS)**是应用程序的实际内存使用量,考虑到了共享内存的部分。这是最重要的属性

Shared Dirty: 这是应用程序与其他应用程序共享的脏内存。

Shared Clean: 这是应用程序与其他应用程序共享的干净内存。

Swapped: 这是已被交换到磁盘上的内存量。

Heap Size: Dalvik或ART堆的总大小。

Allocated: 已分配的堆内存量。

Free: 堆中可用的空闲内存量。

Zygote: Zygote进程的内存信息,Zygote是应用程序孵化器。

System: 系统进程的内存信息。

Total RAM: 设备的总RAM大小。

Total Swap: 设备的总交换空间大小。
这里最重要的就是

4.可以写一个python脚本,来监控对应进程的CPU和内存情况:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os
import time
import sys
import warnings
import platform

if 'Windows' in platform.system():
    cmd_mem_tpl = "adb shell dumpsys meminfo %s |findstr TOTAL:"
    cmd_cpu_tpl = "adb shell top -d 1 -n 1 -m 1000 -s 6 -o PID,PR,RES,SHR,S,%%CPU,%%MEM,ARGS|findstr %s"
else:
    cmd_mem_tpl = "adb shell dumpsys meminfo %s |grep TOTAL"
    cmd_cpu_tpl = "adb shell top -d 1 -n 1  -s 6 -o PID,PR,RES,SHR,S,%%CPU,%%MEM,ARGS|grep %s"

warnings.simplefilter("ignore")
# cmd_user_sys="adb shell top -n 1 -m 1|findstr host"
stat_tpl = "%s【CPU】实时:%s%% 均值:%s%% 峰值:%s%% 【内存RES】实时:%sM 均值:%sM 峰值:%sM【内存PSS】实时:%sM 均值:%sM 峰值:%sM 历时:%s 次数:%d"


# def parse_args():
#     global app_name
#     app_name= sys.argv[1]


def excecmd(cmd):
    r = os.popen(cmd)
    text = r.readline().split()
    return text


def get_spend_time(start_time):
    total_time = time.time() - start_time
    m, s = divmod(total_time, 60)
    h, m = divmod(m, 60)
    return "%02d:%02d:%02d" % (h, m, s)


def cur_time():
    return time.strftime('%H:%M:%S', time.localtime(time.time()))


def cpu_mem(app_name, seconds):
    result_file = open("进程性能监控数据.txt", 'w')
    total_cpu = 0
    total_mem_pss = 0
    total_mem_res = 0
    max_cpu = 0
    max_mem_pss = 0
    max_mem_res = 0
    stat_count = 0
    cmd_cpu = cmd_cpu_tpl % app_name
    cmd_mem = cmd_mem_tpl % app_name
    avg_cpu = 0
    avg_mem_res = 0
    avg_mem_pss = 0
    spend_time = ""
    excecmd("adb root")
    print("process: " + app_name + ", time: " + str(seconds) + "s\n")
    result_file.write("process: " + app_name + ", time: " + str(seconds) + "s\n")
    result_file.write("时间\tCPU\tRES\tPSS\n")
    start_time = time.time()
    while (time.time() - start_time) < seconds:
        try:
            top_res = excecmd(cmd_cpu)
            dumpsys_res = excecmd(cmd_mem)
            cpu = float(top_res[5])
            if cpu > max_cpu:
                max_cpu = cpu
                pass
            mem_res = float(top_res[2][0:-1])
            if mem_res > max_mem_res:
                max_mem_res = mem_res
                pass
            # cmd = dumpsys_res[0][0:-2].split(",")
            # mem_pss = float(cmd[0] + cmd[1]) / 1024
            mem_pss = float(dumpsys_res[1]) / 1024
            if mem_pss > max_mem_pss:
                max_mem_pss = mem_pss
                pass
            total_cpu += cpu
            total_mem_res += mem_res
            total_mem_pss += mem_pss
            stat_count += 1
            avg_mem_res = total_mem_res / stat_count
            avg_cpu = total_cpu / stat_count
            avg_mem_pss = total_mem_pss / stat_count
            spend_time = get_spend_time(start_time)
            print(stat_tpl % (
                cur_time(), str(round(cpu, 2)), str(round(avg_cpu, 2)), str(round(max_cpu, 2)),
                str(round(mem_res, 2)), str(round(avg_mem_res, 2)), str(round(max_mem_res, 2)),
                str(round(mem_pss, 2)), str(round(avg_mem_pss, 2)), str(round(max_mem_pss, 2)),
                spend_time, stat_count))
            result_file.write("%s\t%s%%\t%s\t%s\n" % (
                cur_time(), str(round(cpu, 2)), str(round(mem_res, 2)), str(round(mem_pss, 2))))
        except Exception as e:
            print(e)
    result_file.write(
        "总计:【CPU】均值:%s%% 峰值:%s%% 【内存RES】均值:%sM 峰值:%sM【内存PSS】均值:%sM 峰值:%sM 历时:%s 次数:%d\n" % (
            str(round(avg_cpu, 2)), str(round(max_cpu, 2)),
            str(round(avg_mem_res, 2)), str(round(max_mem_res, 2)),
            str(round(avg_mem_pss, 2)), str(round(max_mem_pss, 2)),
            spend_time, stat_count))
    result_file.close()
    return


if __name__ == "__main__":
    try:
        process = sys.argv[1]
        time_second = int(sys.argv[2])
    except:
        process = "com.google.mail"  # 进程
        time_second =600  # 时长,秒单位
    sys.exit(cpu_mem(process, time_second))

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/773203
推荐阅读
相关标签
  

闽ICP备14008679号