您的位置:首页 > 运维架构 > Shell

eclipse 用adb shell ps 命令检测某个应用的资源占用

2017-04-01 19:37 330 查看
最近学习monkey的时候,了解了一下关于内存溢出的知识,可惜用eclipse的DDMS只能获取到debug调试的进程,所以就写了一个简单的获取某个包占用内存的方法,分享出来,供大家参考。

for(int i=0;i<500;i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
execCmd("cmd /c adb shell ps");
}

execCmd的方法改了一下,也可以用参数来控制查询的报名。
public static void execCmd(String cmd) {
System.out.println("----execCmd: " + cmd);
try {
Process p = Runtime.getRuntime().exec(cmd);
// 正确输出流
InputStream input = p.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line = "";
while ((line = reader.readLine()) != null) {
if (line.contains("gaotu")) {
System.out.println(line);
}
// System.out.println(line);
saveToFile(line, "runlog.log", false);
}
// 错误输出流
InputStream errorInput = p.getErrorStream();
BufferedReader errorReader = new BufferedReader(new InputStreamReader(errorInput));
String eline = "";
while ((eline = errorReader.readLine()) != null) {
System.out.println(eline);
saveToFile(eline, "runlog.log", false);
}
} catch (IOException e) {
e.printStackTrace();
}
}saveToFile的方法如下
public static void saveToFile(String text, String path, boolean isClose) {
File file = new File("runlog.log");
BufferedWriter bf = null;
try {
FileOutputStream outputStream = new FileOutputStream(file, true);
OutputStreamWriter outWriter = new OutputStreamWriter(outputStream);
bf = new BufferedWriter(outWriter);
bf.append(text);
bf.newLine();
bf.flush();

if (isClose) {
bf.close();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息