前提条件
- Android版本为8.0以上
环境配置
- cd到/src/main目录下,新建shell目录,同时shell目录下配置与libs目录下相同平台的目录,如下app下的层级结构,可看到shell/lib下具有与libs下相同的平台目录结构
── AndroidManifest.xml
├── java
├── libs
│ ├── arm64-v8a
│ └── armeabi-v7a
├── main.iml
├── res
└── shell
└── lib
├── arm64-v8a
└── armeabi-v7a
- 分别在shell/lib/目录下建立一个wrap.sh脚本文件,编辑wrap.sh文件并写入如下内容
#!/system/bin/sh
LIBC_DEBUG_MALLOC_OPTIONS=backtrace $@
并赋予脚本其执行权限 chmod +x wrap.sh
注意,如果你最终生成的apk是只需要armeabi-v7a的,则不要在arm64-v8a中放置wrap.sh脚本文件。
- 打开/build.gradle文件,在sourceSets.main增加资源文件路径,如
sourceSets.main {
jni.srcDirs = []
jniLibs.srcDir "src/main/libs"
resources.srcDir "src/main/shell"
}
- 环境配置完成,编译打包apk,使用Android Studio分析apk,确保相应平台里存在wrap.sh脚本文件
获取Native内存分配信息
- 安装运行apk,并获取pid
- 对app执行一系列操作,由于此时内存分配时会执行更多的操作,app运行速度可能减慢
- 终端上执行如下命令,
adb shell am dumpheap -n <pid> /data/local/tmp/heap.txt
- 将/data/local/tmp/heap.txt pull到本地,打开heap.txt可看到一些信息,如下
Android Native Heap Dump v1.0
Total memory: 38937197
Allocation records: 42720
Backtrace size: 16
z 1 sz 20039040 num 1 bt edeff75a edeff654 edeff73e ee44d99a efda71f0 ee4487a4
z 1 sz 3908764 num 1 bt d2a28a38 d2a332b0 d290977a d28f9d96
z 1 sz 520192 num 1 bt cd30ca40 cd2b3ce8 cd2c307a cd2c2e50 cd2c2c24 cd2bca9e ee5cbdac ce546204 ce54d022 ce54f95c ce54ad92 ce53c1b4 ce53c1da ce5dab22 ee2ca8d6 ee29e340
......
- 下载文件native_heapdump_viewer.py,如果你终端无法直接访问addr2line和objdump两个命令,则需要将python脚本中两命令替换为ndk的路径访问,如
<ndk path>toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-objdump
<ndk path>toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-addr2line
- 使用如下命令对抓到取的native内存分配信息进行符号分析
python native_heapdump_viewer.py --symbols . heap.txt > heapinfo.txt
这时我以获取libQuCore.so库的内存分配信息为例,--symbols是设置给脚本文件路径,注意此时打开heapinfo.txt可能无法获取到正确的地址和文件匹配信息,比如会收到类似如下的信息
/data/app/com.aliyun.apsaravideo-e1DYx-Wua0VifW_TwlHhAw==/lib/arm/libQuCore.so not found for symbol resolution
2521990 6.72% 92.18% 409 ce5dab22 /data/app/com.aliyun.apsaravideo-e1DYx-Wua0VifW_TwlHhAw==/lib/arm/libQuCore.so ??? ???
2520966 6.72% 99.96% 408 ce53c1da /data/app/com.aliyun.apsaravideo-e1DYx-Wua0VifW_TwlHhAw==/lib/arm/libQuCore.so ??? ???
2519574 6.71% 99.94% 381 ce53c1b4 /data/app/com.aliyun.apsaravideo-e1DYx-Wua0VifW_TwlHhAw==/lib/arm/libQuCore.so ??? ???
2519574 6.71% 100.00% 381 ce54ad92 /data/app/com.aliyun.apsaravideo-e1DYx-Wua0VifW_TwlHhAw==/lib/arm/libQuCore.so ??? ???
2519414 6.71% 99.99% 379 ce54f95c /data/app/com.aliyun.apsaravideo-e1DYx-Wua0VifW_TwlHhAw==/lib/arm/libQuCore.so ??? ???
2517560 6.71% 99.93% 351 ce54d022 /data/app/com.aliyun.apsaravideo-e1DYx-Wua0VifW_TwlHhAw==/lib/arm/libQuCore.so ??? ???
2500576 6.66% 99.33% 160 ce546204 /data/app/com.aliyun.apsaravideo-e1DYx-Wua0VifW_TwlHhAw==/lib/arm/libQuCore.so ??? ???
这是因为android需要与手机库路径完全一样的符号文件路径,于是我们在本地新建一个与上述获取到的手机上的相同的路径并将libQuCore.so拷贝到新建的路径下,假设此路径位于symbols目录下,如symbols/data/app/com.aliyun.apsaravideo-e1DYx-Wua0VifW_TwlHhAw==/lib/arm/libQuCore.so路径存在,重新执行上述符号解析命令python native_heapdump_viewer.py --symbols symbols heap.txt > heapinfo.txt
再次打开heapinfo.txt文件,可以获取libQuCore.so相关的符号与文件、行数匹配的信息了,如下摘取了一部分示例
2521990 6.72% 92.18% 409 ce5dab22 /data/app/com.aliyun.apsaravideo-e1DYx-Wua0VifW_TwlHhAw==/lib/arm/libQuCore.so execute_native_thread_routine /Volumes/Android/buildbot/src/android/ndk-r14-release/toolchain/gcc/gcc-4.9/libstdc++-v3/src/c++11/thread.cc:84
2520966 6.72% 99.96% 408 ce53c1da /data/app/com.aliyun.apsaravideo-e1DYx-Wua0VifW_TwlHhAw==/lib/arm/libQuCore.so std::thread::_Impl<std::_Bind_simple<void (*(alivc::ThreadService*))(alivc::ThreadService*)> >::_M_run() /Users/xunshan/Library/Android/sdk/android-ndk-r14b/sources/cxx-stl/gnu-libstdc++/4.9/include/functional:1700 (discriminator 2)
2519574 6.71% 99.94% 381 ce53c1b4 /data/app/com.aliyun.apsaravideo-e1DYx-Wua0VifW_TwlHhAw==/lib/arm/libQuCore.so alivc::ThreadWorker(alivc::ThreadService*) /Users/xunshan/Desktop/AliyunSVideo-product/sources/native/modules/alivc_framework/src/mdf/service/thread_service.cpp:18
2519574 6.71% 100.00% 381 ce54ad92 /data/app/com.aliyun.apsaravideo-e1DYx-Wua0VifW_TwlHhAw==/lib/arm/libQuCore.so alivc::RenderEngineService::OnInit() /Users/xunshan/Desktop/AliyunSVideo-product/sources/native/modules/alivc_framework/src/render_engine/render_engine_service.cpp:284
2519414 6.71% 99.99% 379 ce54f95c /data/app/com.aliyun.apsaravideo-e1DYx-Wua0VifW_TwlHhAw==/lib/arm/libQuCore.so alivc::Canvas::Init() /Users/xunshan/Desktop/AliyunSVideo-product/sources/native/modules/alivc_framework/src/render_engine/canvas.cpp:36 (discriminator 1)
2517560 6.71% 99.93% 351 ce54d022 /data/app/com.aliyun.apsaravideo-e1DYx-Wua0VifW_TwlHhAw==/lib/arm/libQuCore.so alivc::GraphicContext::Init(void*) /Users/xunshan/Desktop/AliyunSVideo-product/sources/native/modules/alivc_framework/src/render_engine/render_system/GL/graphic_context.cpp:36
2500576 6.66% 99.33% 160 ce546204 /data/app/com.aliyun.apsaravideo-e1DYx-Wua0VifW_TwlHhAw==/lib/arm/libQuCore.so alivc::AlivcEGLContext::Init(void*) /Users/xunshan/Desktop/AliyunSVideo-product/sources/native/modules/alivc_framework/src/render_engine/render_system/EGL/egl_context.cpp:59
- 针对上述获取到的内存分配对应的文件和行数解析的信息,可以分析native的内存分配是否发生了泄漏及哪些地方导致了内存泄漏