由于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;
}
}
结果: