您的位置:首页 > 移动开发 > Android开发

Android性能专项测试之Allocation Tracker(Device Monitor)

2015-10-15 16:40 281 查看
Allocation Tracker Walkthrough


Allocation Tracker 能做什么?

追踪内存分配信息,按顺序排列,这样我们就能清晰看出来某一个操作的内存是如何一步一步分配出来的。比如在有内存抖动的可疑点,我们可以通过查看其内存分配轨迹来看短时间内有多少相同或相似的对象被创建,进一步找出发生问题的代码。


Allocation Tracker使用条件

Root手机
开发者选项可用


Allocation Tracker面板



各名称的含义如下:
名称意义
Alloc Order分配序列
Allocation Size分配的大小
Allocated Class被分配的对象
Thread Id线程id号
Allocated in在哪个类分配的
第二个Allocated in在哪个方法分配的


Allocation Tracker操作

1.首先进入你要追踪的界面

2.点击
Start Tracking
按钮,开始跟踪内存分配轨迹

3.操作你的界面,尽量时间短点

4.点击
Get Allocations
按钮,抓去内存分配轨迹信息,显示在右边的面板中,默认以内存大小排序,你可以以分配顺序排序或者仍以列排序。

5.logcat中会显示出这次的轨迹共抓到内存分配轨迹记录数,可以简单的理解分配了多少次内存,这个数值和Alloc order的最大值是相等的

6.如果你不想看那么多乱七八糟的,你可以使用Filter来过滤,输入包名就可以了。

实例

无任何操作时内存轨迹

打开首页,点击
Stop tracking
,然后点击
Get
Allocations
,会看到下面1~8的内存分配序列:



再按一次
Get Allocations
会出现如下状态:



这些信息估计都是DDMS和app交互产生的内存,我们可以忽略

正常操作的内存轨迹

如果这个时候我们想单独获取某次操作的内存轨迹,首先一定要记得
Stop Tracking
Start
Tracking
一下,让追踪点初始化一下,这个时候我们从首页进入一个详情页,看一下我们的内存分配轨迹:



追踪到的内存分配3823次,看着是不是有点无从下手,没关系,用Filter过滤下:



过滤后,就剩下了跟我们App源码有关系的分配轨迹,我们随便选择一栏,可以看到其trace信息:



上图中,我们可以看出来,在第2415次内存分配中,分配的是
DetailFragment
对象,占用内存272字节,处理线程Id为1,在
com.example.android.sunshine.app.DetailActivity
onCreate
方法中分配的。从trace信息可以看出来该方法一步一步被调用的信息。

然后我们回源码中确认下,以下代码就是我们上面选择的内存分配的地方:
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">private final String LOG_TAG = DetailActivity<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.class</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getSimpleName</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

@Override
protected void onCreate(Bundle savedInstanceState) {
super<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.onCreate</span>(savedInstanceState)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
setContentView(R<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.layout</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.activity</span>_detail)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
Log<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.d</span>(LOG_TAG, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"onCreate"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
ActivityManager<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getInstance</span>()<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.registerActivity</span>(this)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
if (savedInstanceState == null) {
// Create the detail fragment <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">add</span> it to the activity
// using a fragment transaction.

Bundle arguments = new Bundle()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
arguments<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.putParcelable</span>(DetailFragment<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.DETAIL</span>_URI, getIntent()<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getData</span>())<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

DetailFragment fragment = new DetailFragment()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
fragment<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.setArguments</span>(arguments)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>

getSupportFragmentManager()<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.beginTransaction</span>()
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.add</span>(R<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.id</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.weather</span>_detail_container, fragment)
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.commit</span>()<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
}
}
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li></ul>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: