java代码通过adb检测应用程序内存状况

10 篇文章 0 订阅
1 篇文章 0 订阅

由于app有的时候大概需要看看应用的app内存占用量,所以这里自己写了个方法,不断通过adb查询android程序的内存使用情况。代码如下:

public class CallCmd {
public static void main(String[] args) {


CallCmd cc=new CallCmd();
String applicationPackageName="cn.com.package.name";//需要查询的app的包名
String commandGetAppAllMemory="adb shell dumpsys meminfo --oom | grep \""+applicationPackageName+"\"";
String commandApplicationId="adb shell ps | grep "+applicationPackageName;
String commandTotalFreeMeminfo="adb shell dumpsys meminfo | grep Free";
final String  commandGetAppMemoryInfo="adb shell dumpsys meminfo ";


String commandMusic="";
String id="";
id=cc.getId(commandApplicationId);
commandMusic=commandGetAppMemoryInfo+id;
while(true){
String totalFree=cc.getMemory(commandTotalFreeMeminfo).toString().trim();
System.out.println(totalFree);
String result=cc.getMemory(commandMusic).toString().trim();
if(result==null||result.equals("")||result.startsWith("No process found for:")){
id=cc.getId(commandApplicationId);
commandMusic=commandGetAppMemoryInfo+id;
}else{
cc.outMemoryInfo(result);
}
try {
Thread.sleep(1000);//查询间隔
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}


public String getId(String command){
String id=getMemory(command).toString();
if(id==null||id.equals("")){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return getId(command);
}
System.out.println("getid :"+id.split("\\s+")[1]);
return id.split("\\s+")[1];// \\s表示回车换行等字符。而“ ”只代表空格 

}
public void outMemoryInfo(String meminfo){
//System.out.println("meminfo:"+meminfo);
String[] tmp=meminfo.split("\\s+");
String nativeHeapPrivateDirty;
String nativeHeapSize;
String nativeHeapAlloc;
String nativeHeapFree;

String dalvikHeapPrivateDirty;
String dalvikHeapSize;
String dalvikHeapAlloc;
String dalvikHeapFree;

String GfxDev="";
String totalPss;

int length=tmp.length;
SimpleDateFormat df=new SimpleDateFormat("HH:mm:ss:SSS");
System.out.println("time:"+df.format(System.currentTimeMillis()));
for(int i=0;i<length;i++){

if(tmp[i].equals("Native")&&tmp[i+1].equals("Heap")){
i+=3;
nativeHeapPrivateDirty=tmp[i];
i+=3;
nativeHeapSize=tmp[i];
i++;
nativeHeapAlloc=tmp[i];
i++;
nativeHeapFree=tmp[i];
System.out.println("nativeHeapPrivateDirty :"+nativeHeapPrivateDirty +" nativeHeapSize:"+nativeHeapSize
+" nativeHeapAlloc:"+nativeHeapAlloc+" nativeHeapFree:"+nativeHeapFree);
}else if(tmp[i].equals("Dalvik")&&tmp[i+1].equals("Heap")){
i+=3;
dalvikHeapPrivateDirty=tmp[i];
i+=3;
dalvikHeapSize=tmp[i];
i++;
dalvikHeapAlloc=tmp[i];
i++;
dalvikHeapFree=tmp[i];
System.out.println("dalvikHeapPrivateDirty :"+dalvikHeapPrivateDirty +" dalvikHeapSize:"+dalvikHeapSize
+" dalvikHeapAlloc:"+dalvikHeapAlloc+" dalvikHeapFree:"+dalvikHeapFree);
}else if(tmp[i].equals("Gfx")){
i+=2;
GfxDev=tmp[i];
}else if(tmp[i].equals("TOTAL")&&tmp[i+1].matches("^[0-9]+")){
totalPss=tmp[i+1];
System.out.println("GfxDev:"+GfxDev+" totalPss:"+totalPss);
break;
}
}

}
public StringBuilder getMemory(String command){
BufferedReader br = null;
StringBuilder sb=null;
try {
Process p = Runtime.getRuntime().exec(command);
br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
} finally {


if (br != null) {
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return sb;
}
}

结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值