获取内存 adb shell top
adb shell top -d 5 >meminfo (-d:刷新频率 5:是1秒刷新一次)
获取指定app内存 adb shell dumpsys meminfo packageName
VSS - Virtual Set Size 虚拟耗用内存
RSS - Resident Set Size 实际使用物理内存
通过脚本定期采集数据,将结果做一个曲线图,如果曲线图波动不大且稳定,可以认定为app无内存泄漏情况
import os import time import csv class Controller(object): def __init__(self,count): #定义收集数据的数组 self.alldata = [("count","vss","rss")] os.popen("adb shell top -d 5 >meminfo") #meminfo 是到处到adb的目下 def analyzedata(self): content = self.readfile() i = 0 for line in content: if "com.example.shineapp" in line : print(line) line = "#".join(line.split()) vss = line.split("#")[5].strip("K") rss = line.split("#")[6].strip("K") #将数据存到数组里 self.alldata.append((i,vss,rss)) i = i+1 #数据的存储 def SaveDataToCsv(self): csvfile = open("meminfo.csv","wb") writer = csv.writer(csvfile) writer.writerows(self.alldata) csvfile.close() def readfile(self): mfile = open("meminfo","r") content = mfile.readlines() mfile.close() return content if __name__ == '__main__': controller = Controller() controller.analyzedata() controller.SaveDataToCsv()