定义: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