How can I use GC using adb tools?
The DDMS debugger connects to the Dalvik VM over a socket. In response to the "GC Reason" command, it sends a "HPGC" (ie HeaP GC) packet that ends in a handleHPGC()
class method core/java/android/ddm/DdmHandleHeap.java
.
The method handleHPGC()
just calls the method java.lang.Runtime.getRuntime().gc()
.
I think you could use the Java Debug Wire Protocol (JDWP) to write a simple (??? - I've never done that, so it might not be that simple) program that will attach to the debug port for the application you are test and call the garbage collector using this API.
How Java docs say forjava.lang.Runtime.gc()
:
A method
System.gc()
is a common and convenient means of calling this method.
source to share
kill -10 pid
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!");
}
}
source to share
Determining the cause of GC in DDMS: -
On the Heap tab, select GC Reason to trigger garbage collection, which allows you to collect heap data. When the operation is complete, you will see a group of object types and the memory that has been allocated for each type. You can hit Cause GC again to refresh the data.
This is called GC_EXPLICIT
Use Debug.dumpHprofData (String) to generate a heap dump.
Dump "hprof" data to the specified file. This can cause GC.
source to share