2014-07-29

Traceview Introduction

Traceview Introduction


Traceview is a performance debug tool provided in Android SDK. Traceview is a graphical viewer for execution logs saved by your application. Traceview can help you debug your application and profile its performance. It can trace into a specific method performance.

Creating Traceview Files


There are two ways to create Traceview log:

1. Using android.os.Debug class to generate trace log:

  • Make sure that your application must have permission to write to external storage.

For example, add below line in AndroidMainfest<a rel="nofollow" href="edit.SEABJ.AndroidMainfest?topicparent=SEABJ.HowToUseTraceviewOnNormandy" title="Create this topic">?</a>.xml of Launcher,

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

  • Include android.os.Debug class in your code, and specify where to start and stop logging trace in your code by calling methods Debug.startMethodTracing() and Debug.stopMethodTracing(). In method Debug.startMethodTracing(), you can specify the trace file’s name (default directory /sdcard/).

For example, in LauncherActivity<a rel="nofollow" href="edit.SEABJ.LauncherActivity?topicparent=SEABJ.HowToUseTraceviewOnNormandy" title="Create this topic">?</a>.java,

import android.os.Debug;

public void onCreate(Bundle state) {

Debug.startMethodTracing("home");

...

}

public void onAttachedToWindow() {

...

Debug.stopMethodTracng();

}

2. Using the Method Profiling feature of DDMS:

This is very simple, no need to change code, but less precise than the first method . To start method profiling:

  • On the Devices tab, select the process that you want to enable method profiling for.
  • Click the Start Method Profiling button.
  • Interact with your application to start the methods that you want to profile.
  • 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.

Viewing Traceview Files


To start Traceview, enter the following command from the SDK tools/ directory:

Traceview home.trace

 

No comments: