Traceview是android平台配备的一个很好的性能分析工具。它可以通过图形化的方式让我们了解我们要跟踪的程序的性能,并且能具体到method。

使用Traceview的版本限制

对于Android 1.5及以下的版本:不支持。

对于Android 1.5以上2.1下(含2.1)的版本:受限支持。trace文件只能生成到SD卡,且必须在程序中加入代码。

对于Android 2.2上(含2.2)的版本:全支持。可以不用SD卡,不用在程序中加代码,直接自己用DDMS就可以进程Traceview。

一、Android 1.5以上2.1下(含2.1)的版本中Traceview的使用

首先,必须在程序当中加入代码,以便生成trace文件,有了这个trace文件我们才可以将其转化为图形。

1.1、启动追踪

使用Debug的以下静态方法方法来启动:

static void startMethodTracing(String traceName)

Start method tracing, specifying the trace log file name.

使用指定trace文件的名字和默认最大容量(8M)的方式开始方法的追踪

static void startMethodTracing()

Start method tracing with default log name and buffer size.

使用默认trace文件的名字(dmtrace.trace)和默认最大容量(8M)的方式开始方法的追踪

static void startMethodTracing(String traceName, int bufferSize, int flags)

Start method tracing, specifying the trace log file name and the buffer size.

使用指定trace文件的名字和最大容量的方式开始方法的追踪。并可指定flags.

注:int flags好像没意义。一般都用0.

static void startMethodTracing(String traceName, int bufferSize)

Start method tracing, specifying the trace log file name and the buffer size.

使用指定trace文件的名字和最大容量的方式开始方法的追踪。

注1:以上的方法的文件都会创建于SD卡下,即"/sdcard/"下,对默认文件名的就是"/sdcard/dmtrace.trace"

如果没SD卡,以上方法会抛异常致使程序crash.

注2:如果文件名没有指定类型,系统为其加上类型.trace

1.2、停止追踪

使用Debug的静态方法方法来停止:

public static void stopMethodTracing ()。

例如在activity的onCreate()中添加Debug.startMethodTracing(), 而在onDestroy()中添加Debug.stopMethodTracing(),

如下:

@Overridepublic void onCreate(Bundle savedInstanceState) {Debug.startMethodTracing();super.onCreate(savedInstanceState);..............................}protected void onDestroy() {super.onDestroy();.................Debug.stopMethodTracing();}

对于模拟器我们还得创建一个带有SD card的AVD,这样才能使trace文件保存到/sdcard/...当中。

可以在命令中分别单独创建,也可以在创建avd的时候一起将sdcard创建。创建之后通过DDMS file explore我们就可以看到/sdcard/目录下有一个trace文件,如果没有在Debug语句中设置名字则默认为dmtrace.trace.

注意:需要在AndroidManifest.xml文件中添加android.permission.WRITE_EXTERNAL_STORAGE权限。

1.3、 把trace文件从SD卡拷到电脑上

现在我们把这个文件copy到我们的电脑上指定的目录:

adb pull /sdcard/dmtrace.trace d:

1.4、启动traceview可视界面

现在就可以通过命令行来执行traceview了。进入SDK的tools目录后,执行traceview,如下:

traceview D:\dmtrace.trace.

之后即可以看到图形界面了。

1.5、分析traceview结果

Timeline Panel

窗口的上半部分是时间轴面图(Timeline Panel)

The image below shows a close up of the timeline panel. Each thread’s execution is shown in its own row, with time increasing to the right.

Each method is shown in another color (colors are reused in a round-robin fashion starting with the methods that have the most inclusive time).

The thin lines underneath the first row show the extent (entry to exit) of all the calls to the selected method.

界面上方的尺子代表了MethodTracing的时间段(从Debug.startMethodTracing()到Debug.stopMethodTracing()的时间)。

每个线程的函数执行时间图处于和线程名同一行的右侧。

注1:线宽度代表执行该函数本身操作所用的时间

注2:函数所调用的子函数时间线夹杂在该函数本身操作所用的时间线之间。

注3:时间线的高度不知道有什么意义。

注4:函数本身是嵌套的。

注5:每行下面粗的线段标注了Profile Panel中被选中函数调用所消耗的时间段。每个线段对应一次函数的运行。

在下面的图中我们可以看到有11次对LoadListener.nativeFinished()的调用,其中有一次调用耗时特别的多。这里的11次和图2并不一致,应该是google的疏忽。

图1The Traceview Timeline Panel

Profile Panel

窗口的下半界面是对各个函数调用的汇总图Profile Panel

Figure 2 shows the profile pane, a summary of all the time spent in a method.

The table shows both the inclusive and exclusive times (as well as the percentage of the total time).

Exclusive time is the time spent in the method.

Inclusive time is the time spent in the method plus the time spent in any called functions.

We refer to calling methods as "parents" and called methods as "children." When a method is selected (by clicking on it),

it expands to show the parents and children. Parents are shown with a purple background and children with a yellow background.

The last column in the table shows the number of calls to this method plus the number of recursive calls.

The last column shows the number of calls out of the total number of calls made to that method.

In this view, we can see that there were 14 calls to LoadListener.nativeFinished();

looking at the timeline panel shows that one of those calls took an unusually long time.

图2是对各个函数调用的汇总图Profile Panel。该表给出了the inclusive and exclusive times及他们所占有的百分比。

Exclusive time是该函数本身基本操作不包括子函数调用)的时间。

Inclusive time是该函数调用所用的时间(包括子函数调用)的时间。

列1:"Name"表示函数名。

双击函数名,可以看到在上半界面是时间轴面图(Timeline Panel)看他的所消耗的时间段。(用粗的线段标注)。

双击函数名左边的"+"展开后可以看到,该函数的"parents"和"children"

列2:"incl%"表示函数的Inclusive time整个MethodTracing时间里占的百分比

列3:"Inclusive"表示Inclusive time

列4:"Excl%"表示函数的Exclusive time整个MethodTracing时间里占的百分比

列5:"Exclusive"表示Exclusive time

列6:"Calls+RecurCalls/Total"表示对函数的调用次数包括递归调用)。如图2的nativeFinished()那列为"14+0"表示14次非递归调用,0次递归调用.

列7:新的版本(比如2.1)还有"time/calls"表示平均的调用时间(即Inclusive time/ total calls)。如图3。来自google文档的图2感觉有老了。

:如果函数A调用函数B那么函数A称为函数B的"parents",函数B称为函数A的"children."

图2Profile Panel

图3Profile Panel

二、Android 2.2以上(含2.2)的版本中Traceview的简化使用。

Android 2.2除了像2.1当中那样使用Traceview,还可以在DDMS使用Traceview,比以前简化的不少。

* The trace log files are streamed directly to your development machine.

在Android 2.2后可以不用SD卡。Traceview文件流是直接指向开发调试的主机(即DDMS所在的电脑)

To start method profiling:

1. On the Devices tab, select the process that you want to enable method profiling for.

2. Click the Start Method Profiling button.

3. Interact with your application to start the methods that you want to profile.

4. Click the Stop Method Profiling button. DDMS stops profiling your application and opens Traceview with the method profiling information that was collected between the time you clicked on Start Method Profiling and Stop Method Profiling.

在DDMS中如何进行Traceview。

1,在设备表中选中你想进行method trace的进程

2,单击Method Profiling按钮开始method trace。该按钮在窗口的左上方,它在"stop"按钮的左方,"Device菜单的正下方"。

3,method trace进行中

4,单击Method Profiling按钮停止method trace。紧接着系统会自动弹出Traceview窗口来显示刚才的method trace结果。

三、Traceview文件格式

关于Traceview文件格式请参照http://developer.android.com/guide/developing/debugging/debugging-tracing.html

本文参照来源:

http://wbdban.javaeye.com/blog/564309

http://developer.android.com/guide/developing/debugging/debugging-tracing.html

http://developer.android.com/guide/developing/debugging/ddms.html#profiling

本文转自:http://hubingforever.blog.163.com/blog/static/17104057920112825035143/

转载于:https://www.cnblogs.com/jeriffe/articles/2748153.html

【转】Traceview的使用相关推荐

  1. traceview android studio,TraceView 的正确打开方式

    引言 TraceView 是 Android SDK 提供的一个性能分析工具官网介绍 ,一般用来检查 UI 卡顿.分析 app 耗时操作.但是对于大多说 Android 开发来说,TraceView ...

  2. 正确使用 Android 性能分析工具——TraceView

    前面唠叨 最近公司app中有些列表在滑动的时候会有卡顿现象,我就开始着手解决这些问题,解决问题之前首先要分析列表滑动的性能瓶颈在什么地方.因为之前不会正确使用TraceView这个工具,主要是看不懂T ...

  3. 【Android游戏开发之十】(优化处理)详细剖析Android Traceview 效率检视工具

    本站文章均为 李华明Himi 原创,转载务必在明显处注明: 转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/android-game/316.html 由 ...

  4. 性能分析工具 Android TraceView

    1. TraceView 简介 Traceview是android平台配备一个很好的性能分析的工具.它可以通过图形化的方式让我们了解我们要跟踪的程序的性能,并且能具体到每个方法的执行时间 Tracev ...

  5. Android 常用的性能分析工具详解:GPU呈现模式, TraceView, Systrace, HirearchyViewer(转)...

    此篇将重点介绍几种常用的Android性能分析工具: 一.Logcat 日志 选取Tag=ActivityManager,可以粗略地知道界面Displaying的时间消耗.当我们打开一个Activit ...

  6. 【App性能】:TraceView分析法

    抓取traceview的日志有两种方式, 1,是在代码中片段中添加: Debug.startMethodTracing("hello");.......Debug.stopMeth ...

  7. TraceView(profile) and Systrace

    TraceView(profile) 代码里面 拖到AS ----------------------------------------------------------------------- ...

  8. TraceView 使用详解 android eclipse

    先看命令 (配置好环境变量的情况下,直接traceview+空格+ trace文件路径即可): TraceView是什么 Traceview是android平台配备一个很好的性能分析的工具.它可以通过 ...

  9. Android 系统性能优化(76)--- 如何抓取traceview?

    如何抓取traceview? 在测试性能问题时,需要抓取某个app的traceview,以便从method级别做profiling,traceview正是这样一个提供method profiling的 ...

最新文章

  1. EOSRAM那么火,BANCOR协议白皮书了解一下?
  2. 排球计分程序(三)—— 模型类的设计与实现
  3. mysql数据类型所占空间大小
  4. Linux:CentOS7防火墙基本操作
  5. android studio : clang++.exe: error: invalid linker name in argument '-fuse-ld=bfd
  6. 市场活动课件:SQL Server 索引优化
  7. springboot整合mongodb changestream
  8. power bi 参数_参数化Power BI报表入门
  9. MySQL用户管理和权限设置
  10. win10 开机启动_win10 -- 取消不需要的开机启动项和服务项加快win10系统开机速度...
  11. 信号处理VMD 变分模态分解,示例+完整代码
  12. 普林斯顿微积分读本篇六:求解微分问题
  13. 图灵工业机器人说明书_图说人工智能:机器人极简史
  14. 灵雀云Kube-OVN:基于OVN的开源Kubernetes网络实践
  15. pyecharts世界地图用:国家中英文对照表
  16. 奥城大学计算机科学专业,我是学工程专业本科毕业,托福70分,GRE1020分,都很低,我想去美国留学马上走,...
  17. PTAL1-087机工士姆斯塔迪奥
  18. 正则表达式匹配所有以小写字符开头的单词
  19. INOVIO携手艾棣维欣商业化新冠候选疫苗
  20. 官宣 .NET 6 预览版 6

热门文章

  1. php多文件上传类源码,PHP单文件上传类或多文件上传类源码
  2. VC中使用Matlab Engine出现无法找到libeng.dll的问题
  3. vivado中设置多线程编译
  4. php生成文字闪烁,如何用jquery实现闪烁文字效果
  5. neural network神经网络识别手写字体
  6. FactoryMethod工厂方法模式升级成AbstractFactory抽象工厂模式
  7. System.Security.Cryptography.CryptographicException,密钥集不存在
  8. MySQL主主双机负载均衡
  9. 高可用集群技术之heartbeat应用详解(一)
  10. C语言 数据结构与算法 一