Login or Sign up

Android 性能分析

Posted by: tigerz on March 6, 2010

找到软件系统里最费时的代码,是提高系统性能的一个直接方法。

目前各种语言和平台基本上都有相应的工具做这种分析,Android 上也提供了一个 Traceview 工具。

Traceview 工作方式

要使用 Traceview,首先要生成 trace 文件。在需要跟踪的代码开始前调用 Debug. startMethodTracing(),在结束处调用 Debug.stopMethodTracing()。通常可以在 onCreate() 中调用 startMethodTracing(),在 onDestroy() 中调用 stopMethodTracing()。例如:

// start tracing to "/sdcard/babytracker.trace"
Debug.startMethodTracing("babytracker");
// ...
// stop tracing
Debug.stopMethodTracing();

如果是在模拟器上生成 trace 文件,需要先创建一个 sd 文件。

生成 trace 文件后,把文件导出,然后即可用 traceview 工具查看。

其中 Inclusive time 表示函数和被它调用的函数总用时,Exclusive time 表示函数本身用时。

在 startMethodTracing() 调用后,程序的性能会降低,所以不要在最后发行版里使用。不过可以分析出函数的相对用时。

Comments on This Post:

Please Login (or Sign Up) to leave a comment