How to automate the DDMS snapshot mechanism?

后端 未结 4 825
鱼传尺愫
鱼传尺愫 2020-12-18 10:20

Does Android DDMS provide APIs ? I need to automate the snapshot mechanism for the native heap. Or is there any post processing tool for analysis DDMS native heap snapshots.

相关标签:
4条回答
  • 2020-12-18 11:07

    @laalto's answer is not quite correct

    From a shell you can do the following to get a heap dump for the application using adb.

    Note: Replace 19000 with the process ID of your running application. The filepath must be a filepath which your application has write access to on the Android device.

    Create a heap dump:
    adb shell am dumpheap 19000 /sdcard/Documents/android.hprof
    Pull the file to your machine:
    adb pull /sdcard/Documents/android.hprof
    Convert to a hprof file readable by an analyzer:
    hprof-conv android.hprof mat.hprof

    Tips: Get process ID of your application:

    adb shell ps | grep com.sample.application | cut -c10-15

    Get process ID and dump heap:

    adb shell am dumpheap `adb shell ps | grep com.sample.application | cut -c10-15` /sdcard/Documents/android.hprof

    0 讨论(0)
  • 2020-12-18 11:11

    I assume by snapshot you mean a heap dump.

    From your app code you can call Debug.dumpHprofData() to dump the data.

    From a script you can call

    adb shell am dumpheap <process> <file>
    

    where <process> is e.g. your process id or name, and <file> is the dump file name. After that you can adb pull the dump to your computer.

    To analyze the heap dumps you can use e.g. jhat or MAT. Before that you need to run hprof-conv (included in Android SDK) on the dump to convert it from Dalvik format to standard Java format.

    Further reading: Memory Analysis for Android Applications

    0 讨论(0)
  • 2020-12-18 11:15

    I wrote small script, maybe you would find it useful

    heap_dump_location='/data/local/tmp/tmp.hprof'
    
    dump_heap() {
      adb shell rm $heap_dump_location
      pid=`adb shell ps | grep 'com.example.packagename' | grep -v 'packagename\.' | cut -c10-15`
      adb shell am dumpheap $pid $heap_dump_location
      echo "Heap dump started, we have no idea when it's done, so take a look at logs, and when is done use pull_heap_dump"
    }
    
    pull_heap_dump() {
      adb pull $heap_dump_location $1
    }
    

    https://gist.github.com/logcat/8aeca0ee81af6fb0dc10bb0d58940007

    0 讨论(0)
  • 2020-12-18 11:16

    The DDMS provides a UI for the ADB. You can use ADB commands directly and process the output. The ADB documentation can be found here: http://developer.android.com/tools/help/adb.html

    0 讨论(0)
提交回复
热议问题