1、Android端内存测试,可以通过adb命令:
adb shell dumpsys meminfo <package_name>
Dalvik : dalvik使用的内存
Native : native堆上的内存
Other dev: 除了dalvik和native的内存
Pss : 指将共享内存按比例分配到使用了共享内存的进程
Alloc: 已使用的内存
Free : 空闲的内存
Private Dirty : 非共享,又不能被换页出去的内存
Share Dirty : 共享,但有不能被换页出去的内存
Total 的 PSS 信息,这个值就是应用正真占用的内存大小
重点关注如下几个字段:
1) Native/Dalvik 的 Heap 信息,具体在上面的第一行和第二行,如果发现这个值一直增长,则代表程序可能出现了内存泄漏。
2) Total 的 PSS 信息,这个值就是你的应用真正占据的内存大小,通过这个信息,你可以轻松判别手机中哪些程序占内存比较大了。
2、Android端CPU测试
1)使用android提供的adb shell dumpsys cpuinfo |packagename来获取当前CPU使用率
2)使用top命令 adb shell top |grep packagename 来获取实时CPU使用率
top命令中每项参数含义:
PID:应用程序在系统中的ID;
CPU%:当前瞬时所以使用CPU占用率;
S:进程的状态,
其中S表示休眠,R表示正在运行;
Z:表示僵死状态,N表示该进程优先值是负数;
#THR:程序当前所用的线程数;
VSS : 虚拟耗用内存;
RSS : 实际使用物理内存;
PCY:前台(fg)和后台(bg)进程;
UID:运行当前进程的用户ID;
Name : 应用程序名称。
可以通过Python2脚本输出内存和CPU情况:
使用系统cmd命令,在一定时间内操作应用,实时监控内存CPU变化,最后计算出内存均值和峰值。
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 | # encoding:utf-8 import os import tempfile import time import re def run_once_mem(cmd): # out_file = 'mem_out.tmp.txt.%s' % time.time() # err_file = 'mem_err.tmp.txt.%s' % time.time() # cmd = '%(cmd)s 1>>%(out)s 2>>%(err)s' % { # 'cmd': cmd, # # 'out': out_file, # 'err': err_file # } # return_code = os.system(cmd) # # f1 = open(out_file) # stdout = f1.read() # f2 = open(err_file) # stderr = f2.read() # f1.close() # f2.close() result = os.popen(cmd) stdout = result.read() return stdout, 0 , 0 def run_once_cpu(cmd): # out_file = 'cpu_out.tmp.txt.%s' % time.time() # err_file = 'cpu_err.tmp.txt.%s' % time.time() # cmd = '%(cmd)s 1>>%(out)s 2>>%(err)s' % { # 'cmd': cmd, # 'out': out_file, # 'err': err_file # } # return_code = os.system(cmd) # f1 = open(out_file) # stdout = f1.read() # f2 = open(err_file) # stderr = f2.read() # f1.close() # f2.close() result = os.popen(cmd) stdout = result.read() return stdout, 0 , 0 i = 0 file_name = 'res2.txt' if os.path.exists(file_name): os.remove(file_name) # f = open(file_name, 'a+') summem = countmem = 0 sumcpu = countcpu = 0 time1 = time.time() tempcpu = 0 tempmem = 0 # import os # result = os.popen("ipconfig") # print (result.read()) while i < 250 : #根据时间修改 tempsum = 0 i = i + 1 # time.sleep(1) cmdmem = 'adb shell dumpsys meminfo <package_name>' #修改自己的应用 outmem, std_errmem, codemem = run_once_mem(cmdmem) cmdcpu = 'adb shell top -n 1 -d 0.02 |findstr <package_name>' #修改自己的应用 outcpu, std_err, codecpu = run_once_cpu(cmdcpu) #print(outcpu) print i # print out # print std_err # print code if codemem = = 0 : # f.write(outmem) mem = re. compile ( 'TOTAL[ ]+(\d+)[ ]+.*' ) resmem = mem.findall(outmem) print (resmem) if len (resmem): summem + = int (resmem[ 0 ]) countmem + = 1 if (tempmem < int (resmem[ 0 ])): tempmem = int (resmem[ 0 ]) if codecpu = = 0 : cpu = re. compile ( '(\d+)\%.*' ) rescpu = cpu.findall(outcpu) #print rescpu if len (rescpu): for iter in range ( len (rescpu)): tempsum = tempsum + float (rescpu[ iter ]) print (tempsum) sumcpu + = tempsum countcpu + = 1 if (tempcpu < tempsum): tempcpu = tempsum time2 = time.time() time = time2 - time1 print ( '运行时间:' + str (time) + ' s' ) print ( '内存均值:' + str (summem / countmem / 1024.0 ) + ' MB' ) print ( '内存峰值:' + str (tempmem / 1024.0 ) + ' MB' ) print ( 'cpu均值:' + str (sumcpu / countcpu) + ' %' ) print 'cpu峰值:' + str (tempcpu) + ' %' |
3、iOS端内存CPU测试
使用工具,手机连接电脑,使用xcode-open developer tool - instrument-activity monitor,选择对应的手机对应的应用。查看下方待测应用对应的 %CPU ,CPU time,Real Mem列。
开始后,手机上按照测试用例操作待测应用;
每操作一步,记录real Mem的值 即内存,查看%CPU的值,记录操作过程中的最大值,即峰值;
操作一定时间后,如5分钟;
CPU峰值即为操作过程中记录的最大值;
CPU均值为CPU time/操作时间(S);
内存峰值为操作过程中Real Mem记录的最大值;
内存均值为操作过程中Ream Mem记录下所有值(比如N个值)的和/N。
谢谢查看,持续修改完善!
2019-10-05
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)