最近自动化项目有个需求是关于 GC 操作的,需要在 XX 场景下触发针对某个进程的 GC 操作,类似 DDMS 里的 Cause GC 的作用,是否可以通过 adb 命令来实现呢?有哪位大神做过类似的功能或者了解如何实现,小弟在这里跪拜感激了~~~~~~
这类问题,就是直接没有上下文,直接问的,如果解决了,请在帖子里补充解决方案吧。谢谢
可以, 我不清楚 monitor 是如何实现的. 你可以翻翻他的 jar 代码. 看看细节.
gc 其实只是 System.gc 的一个 api 调用. 所以 jdwp 方法肯定可以做到.
这是在手机端 GC 的,PC 端自行思考吧
private void gc() {
String pid = String.valueOf(ProcessUtils
.getProcessPID(AUTManager.pkn.toString()));
if (!pid.equals("-1")) {
boolean isSucess = true;
ProcessBuilder pb = null;
String cmd = "kill -10 " + pid;
pb = new ProcessBuilder("su", "-c", cmd);
Process exec = null;
pb.redirectErrorStream(true);
try {
exec = pb.start();
InputStream is = exec.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
while ((reader.readLine()) != null) {
isSucess = false;
}
} catch (Exception e) {
e.printStackTrace();
isSucess = false;
}
// 至此命令算是执行成功
if (isSucess)
{
handler.sendEmptyMessage(5);
}
} else {
Log.d("gc error", "pid not found!");
}
}
private void dumpHeap() {
String pid = String.valueOf(ProcessUtils
.getProcessPID(AUTManager.pkn.toString()));
if (!pid.equals("-1")) {
boolean isSucess = true;
ProcessBuilder pb = null;
String sFolder = Env.S_ROOT_DUMP_FOLDER + AUTManager.pkn.toString() + "/";
File folder = new File(sFolder);
if (!folder.exists())
{
folder.mkdirs();
}
String cmd = "am dumpheap " + pid + " "// 命令
+ Env.S_ROOT_DUMP_FOLDER + AUTManager.pkn.toString() + "/"// 输出路径
+ "dump_" + pid + "_" + GTUtils.getSaveDate() + ".hprof"; // 输出文件名
pb = new ProcessBuilder("su", "-c", cmd);
Process exec = null;
pb.redirectErrorStream(true);
try {
exec = pb.start();
InputStream is = exec.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
while ((reader.readLine()) != null) {
isSucess = false;
}
} catch (Exception e) {
e.printStackTrace();
isSucess = false;
}
// 至此命令算是执行成功
if (isSucess)
{
handler.sendEmptyMessage(6);
}
} else {
Log.d("dump error", "pid not found!");
}
}
adb shell am dumpheap
adb shell am dumpheap