Android Native内存分析,systrace,

一,Google官方的调试本地内存工具:

https://source.android.com/devices/tech/debug/native-memory

1.malloc debug:
    adb shell setprop wrap.com.***.***'"LIBC_DEBUG_MALLOC_OPTIONS=backtrace\ leak_track\ logwrapper"'
    adb shell am force-stop com.***.***
    关掉应用,再打开
    adb shell am dumpheap -n PID /data/local/tmp/heap.txt
    开启后,apk运行太慢。
    
    2.malloc hook:
    adb shell setprop wrap.com.***.***'"LIBC_HOOKS_ENABLE=1"'
    adb shell setprop wrap.com.***.***'"LIBC_DEBUG_MALLOC_OPTIONS=backtrace logwrapper"'
    adb shell setprop wrap.com.***.***'"LIBC_HOOKS_ENABLE=1 leak_track logwrapper"'
    adb shell am force-stop com.***.***
    adb shell am dumpheap -n PID /data/local/tmp/heap.txt
    开启后,apk运行太慢。
    
    3.HeapSnapshot:
    https://github.com/albuer/heapsnap

    https://blog.csdn.net/lin20044140410/article/details/107893098
    需要把开源库中的源码编译出可执行文件,库。编译时,开源库中缺少以下依赖库:libbase.so,libc_malloc_debug.so,libc++.so,libdexfile_support.so,libdl.so,liblzma.so,libunwindstack.so
    可以从手机导出,或者下载AndroidAOSP源码自己编译出相关库。
    使用编译出的文件,在Demo中没能检测到native内存泄漏,后续再看看。

    4.Address Sanitizer:
    安装时提示,手机设备不支持。

基于LeakTracer的内存泄漏检测工具:

LeakTracer文档 :
http://www.andreasen.org/LeakTracer/
https://github.com/fredericgermain/LeakTracer

这是我本地调试OK的,可以在android studio上运行的Leak Tracer :https://github.com/whcx/LeakTracerAndroid.git

导出:   libnative_heap.so ,native_heap.txt ,库的名字,dump内存文件名注意跟cmakelist中,

writeLeaksResultToFile方法中的命名一致。

    执行命令:./leak-analyze-addr2line64 libleaktracer.so native_heap.txt >info.txt,解析疑似内存泄漏的调用堆栈。

 

附:调试工具参数

二,MemInfo,

 adb shell dumpsys meminfo pkgname|pid -d
  Private(Clean and Dirty) RAM, 进程使用的内存,进程销毁后系统可回收的RAM容量,
  PSS proportional set size, 实际使用的物理内存,比例分享共享库占用的内存,考虑了进程之间共享RAM的情况,
  DalvikHeap,应用中dalvik分配所占用的RAM,Pss total包括所有zygote分配,private dirty是仅分配给你应用堆得实际RAM,包含了您自己的分配和任何zygote分配页。
  Heap alloc,是dalvik和原生堆分配器为你的应用记录的内存占用量,此值大于pss total, private dirty,因为你的进程是从zygote孵化而来,所以他包含了你的进程和其他的进程的共享分配。
  .so mmap, .dex mmap,这块内存用于映射 .so(native)和.dex(dalvik/art)代码占用的RAM,Pss total 值包括应用之间共享的平台代码,Private clean是你应用自己的代码,
        通常实际映射的内存容量要大得多,此处的RAM只是应用已经执行的代码当前需要占用的RAM。
  .oat mmap,这个等同于代码镜像占用的RAM,它是基于被多个应用预加载的类来计算的,这个镜像在所有应用之间共享,不受特定应用影响。
  .art mmap, 这个等同于堆镜像占用的RAM,它是基于被多个应用预加载的类来计算,此镜像在所有应用之间共享,不受特定应用影响,即使ART镜像包含Object实例,也不会计入你的堆空间。
  Unkown, 系统无法将其分类到其他更具体的一项RAM页,当前,此类RAM页主要包含native分配,工具在收集此数据时无法识别这些分配。
  ViewRootImpl,当前进程中,处于活动状态的跟实图数量,每个根视图都与一个窗口关联。
  AppContexts, Activities, 当前进程中,当前处于活跃状态的应用上下文,和activity对象数量。

 

三,systrace
    

1,adb shell dumpsys gfxinfo com.gritworld.arexample

2,systrace命令行
python版本27.
在C:\Users\jianlin\AppData\Local\Android\Sdk\platform-tools\systrace 目录下执行命令:

python systrace.py -b 16384 -t 8 gfx input view webview sm hal idle freq sched wm am res dalvik disk sync -o mytrace.html

---------------

gfx - Graphics
       input - Input
        view - View System
     webview - WebView
          wm - Window Manager
          am - Activity Manager
          sm - Sync Manager
       audio - Audio
       video - Video
      camera - Camera
         hal - Hardware Modules
         res - Resource Loading
      dalvik - Dalvik VM
          rs - RenderScript
      bionic - Bionic C Library
       power - Power Management
          pm - Package Manager
          ss - System Server
    database - Database
     network - Network
         adb - ADB
    vibrator - Vibrator
        aidl - AIDL calls
         pdx - PDX services
       sched - CPU Scheduling
         irq - IRQ Events
         i2c - I2C Events
        freq - CPU Frequency
        idle - CPU Idle
        disk - Disk I/O
        sync - Synchronization
       workq - Kernel Workqueues
  memreclaim - Kernel Memory Reclaim
  regulators - Voltage and Current Regulators
  binder_driver - Binder Kernel driver
  binder_lock - Binder global lock trace
   pagecache - Page cache

-------------
在app添加自己的systrace log
Trace.traceBegin("performTraversals");
try {
  ……
} finally {
  Trace.traceEnd();
}
需要保证 traceBegin 与 traceEnd 一定要成对出现。 并且一定要在同一个线程里面

------------------

systrace.py 命令参数
  -h, --help            show this help message and exit
  -o FILE               write HTML to FILE
  -t N, --time=N        trace for N seconds
  -b N, --buf-size=N    use a trace buffer size of N KB
  -k KFUNCS, --ktrace=KFUNCS
                        specify a comma-separated list of kernel functions to
                        trace
  -l, --list-categories
                        list the available categories and exit
  -a APP_NAME, --app=APP_NAME
                        enable application-level tracing for comma-separated
                        list of app cmdlines
  --no-fix-threads      don't fix missing or truncated thread names
  --no-fix-tgids        Do not run extra commands to restore missing thread to
                        thread group id mappings.
  --no-fix-circular     don't fix truncated circular traces
  --no-compress         Tell the device not to send the trace data in
                        compressed form.
  --link-assets         (deprecated)
  --boot                reboot the device with tracing during boot enabled.
                        The report is created by hitting Ctrl+C after the
                        device has booted up.
  --from-file=FROM_FILE
                        read the trace from a file (compressed) rather than
                        running a live trace
  --asset-dir=ASSET_DIR
                        (deprecated)
  -e DEVICE_SERIAL, --serial=DEVICE_SERIAL
                        adb device serial number
  --agent-dirs=AGENT_DIRS
                        the directories of additional systrace agent modules.
                        The directories should be comma separated, e.g.,
                        --agent-dirs=dir1,dir2,dir3. Directory |agents| is the
                        default agent directory and will always be checked.
  --target=TARGET       chose tracing target (android or linux)


    
    
    
    
 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值