定义:com.android.server.am.ActivityManagerShellCommand#myRunDumpHeap
int myRunDumpHeap(PrintWriter pw) throws RemoteException {
final PrintWriter err = getErrPrintWriter();
boolean managed = true;
boolean mallocInfo = true;
boolean runGc = true;
String process = "com.example.test2";
String heapFile = "/data/local/tmp/heapdump-" +process+ ".prof";
pw.println("File: " + heapFile);
pw.flush();
File file = new File(heapFile);
file.delete();
ParcelFileDescriptor fd = openFileForSystem(heapFile, "w");
if (fd == null) {
return -1;
}
final CountDownLatch latch = new CountDownLatch(1);
final RemoteCallback finishCallback = new RemoteCallback(new OnResultListener() {
@Override
public void onResult(Bundle result) {
latch.countDown();
}
}, null);
if (!mInterface.dumpHeap(process, UserHandle.USER_CURRENT, managed, mallocInfo, runGc, heapFile, fd,
finishCallback)) {
err.println("HEAP DUMP FAILED on process " + process);
return -1;
}
pw.println("Waiting for dump to finish...");
pw.flush();
try {
latch.await();
pw.println("dump finished...");
} catch (InterruptedException e) {
err.println("Caught InterruptedException");
}
return 0;
}
加入shell cmd 就可以使用如下命令 :
adb shell cmd activity a0
该代码段展示了在Android中如何使用ActivityManagerShellCommand进行堆转储,针对进程com.example.test2生成heapdump文件,并等待转储完成。通过adbshell命令执行此操作,如果成功,会在/data/local/tmp目录下创建heapdump文件。
1万+

被折叠的 条评论
为什么被折叠?



