可可西

Android Studio Profiler(Native Memory)

Android Studio Profiler的Native Memory功能,只用跟踪通过 C/C++ 分配器(包括原生 JNI 对象)进行的分配,不会跟踪Java对象的分配。UE5引擎集成了Android的Native Memory Profiler。

 

以下两种包,可以使用Android Studio的Profile功能:

① Profileable包(Profile时性能高,需要Android 10及以上的系统 ,即TargetSDK要设置为29及以上。需Perfetto来抓取):App中AndroidManifest.xml中插入profileable android:shell="true"

<manifest ...>
    <application ...>
      <profileable android:shell="true" />
    </application>
</manifest>

② 调试包(Profile时性能差):App中AndroidManifest.xml中插入android:debuggable="true"

<manifest ...>
    <application android:debuggable="true" ...>

 

下文使用Android Studio 4.2.1来说明该功能。

Allocation Tracking的方式有:None(不跟踪Allocation)、Sampled(按统计学方法跟踪部分Allocation)和Full(跟踪所有Allocation)。

如上图所示,Full Tracking使用实心圆来表示起点,使用虚线来表示其持续的时长;Sampled Tracking使用空心半圆来表示起点;None Tracking使用空心圆来表示起点。

该选项只影响实时Profiler时的数据获取,Record native allocations不受该选项影响,会抓取所有符合阈值的Native内存分配。

 

profiler配置

注1:Native memory sampling interval (bytes)为抓取的最小内存size,大于等于该值将被memory profiler记录。 该值越小,抓取的数据量越大。uam大厅内存分析用的是512,局内用的是1024。

注2:勾选“Start this recording on startup”,并选择“Native memroy activity(Requires API level >= 29)”,在launch profiler时,会自动开始Recorded Native Allocations

 

launch profiler

自动拉起手机上app,并开始profiler

 

attach profiler

手动在手机上手动启动app,如果app时debuggable的话,会看到该app的进程

 

Table -- Arrange by allocation method视图

注1:Allocations Size,Deallocations Size,Remaining Size的单位为byte 

注2:Remaining Size(分配并没有释放的内存)

         Total Count(分配并没有释放的调用次数)

         Allocations Size(所有分配的调用次数)

         Allocations(所有分配的内存)

注3:!!!0000!开头的是显存相关的分配

 

Table -- Arrange by callstack视图

 

unknown 

// 找不到函数符号

注1:development、debuggame、debug的libUE4.so中本身会包含大量符号,可以直接在堆栈中看到很多函数的名称

注2:test、shipping的libUE4.so在没有debug so时,绝大多数函数会找不到符号,而显示unknown

 

test、shipping的libUE4.so配置debug so的方法

方法①:本地打test、shipping包,然后使用Andriod Studio打开对应的ue4 Android工程,然后launch profiler或attach profiler

方法②:打开之前老的ue4 Android工程,将对应版本的debug libUE4.so拷贝到Intermediate\Android\arm64\gradle\app\src\main\jniLibs\arm64-v8a目录中,然后attach profiler

 

如果Module Name显示libUE4.so!libUE4.so,需要将debug libUE4.so改名为libUE4.so!libUE4.so才能找到符号

 

方法③:对于Profile APK方式,配置debug libUE4.so,test、shipping需要调试一次之后,再launch profiler或attach profiler才能找到符号

 

加载heapprofd文件

要用最新版的Android Studio(如:2021.2.1 Patch 2)来Load from file加载heapprofd文件(需要symbolize过的heapprofd文件),否则libUE4.so会有很多函数为unknown。

 

显示调用图(Call Chart)全貌

① 一共有4个CallStack,栈顶函数分别为main、 __start_thread、unknown、ERROR 1

② 从上到下,形成一个层次结构的CallStack,上面的调用函数(Caller),下面为被调用函数(Callee),与调试器里面看到的CallStack一样

③ 鼠标放在具体的色块栈帧上,会有如下Tips信息:

 

焦点切到调用图(Call Chart)上后,并将输入法切为英文状态

快捷操作 说明 充分展开后(左右方向),快捷操作是否失效
W 放大(左右方向) Yes
S 缩小(左右方向) Yes
A 水平滚动条左移 Yes
D 水平滚动条右移 Yes
鼠标滚轮 垂直滚动条上下移 No
Ctrl+鼠标滚轮 以鼠标所在位置为中心,进行放大缩小(左右方向) Yes

注1:充分展开后(左右方向),只能通过鼠标来拖动水平滚动条进行左右移动

注2:充分展开后(左右方向),快捷操作失效后,可通过将下拉框重新选一下“Total Remaining Size”来恢复成瀑布图全貌

在左侧列表栏的Natvie Sampled上右键,可将数据导出保存到xxxxx.heapprofd文件中

 

参考

使用内存性能分析器查看应用的内存使用情况

Native Memory Profiling with Android Studio 4.1

 

posted on 2021-10-21 23:40  可可西  阅读(5869)  评论(0编辑  收藏  举报

导航