How can I use GC using adb tools?

I want to check memory performance of android apps and I want to get memory information automatically. I am using 'adb shell dumpsys meminfo ...' to get periodic memory information. Now, I want to watch a memory leak, how can I excute gc? Also like "GC Reason" in DDMS. Thank!

+3


source to share


4 answers


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.

+4


source


kill -10 pid

from: https://github.com/TencentOpen/GT/blob/master/android/src/com/tencent/wstt/gt/proInfo/floatView/GTMemHelperFloatview.java



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!");
    }
}

      

+1


source


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.

0


source


adb shell am dumpheap com.test.test /sdcard/test.hprof

      

0


source







All Articles