Listing 121 srccomcookbookandroiddebugtraceviewtestfactorialjava


import; import android.os.Bundle; import android.os.Debug;

public class testfactorial extends Activity { public final String tag="testfactorial";

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); factorial(10);

public int factorial(int n) {


int result=1; for(int i=1; i<=n; i++) { result*=i;


return result;

The factorial() method contains two calls to the Debug class; the trace is started in a file called testfactorial.trace when the startMethodTracing() is called.When the stopMethodTracing() method is called, the system continues buffering the generated trace data. After the method factorial(10) returns, the trace file should be generated and saved in /sdcard/. After the file is generated, it can be retrieved to the development machine using the following command:

> adb pull /sdcard/testfactorial.trace

The traceview tool in the Android SDK tools folder can then be used to analyze the trace file:

> traceview testfactorial.trace

After the script command is run, it produces an analysis screen, as shown in Figure 12.8.

The screen shows a Timeline Panel and a Profile Panel.The Timeline Panel on the top half of the screen describes when each thread and method started and stopped.The Profile Panel on the bottom half of the screen provides the summary of what happened inside the factorial method. By moving the cursor around in the Timeline Panel, it displays the time when the tracing started, when the method was called, and when the tracing ended.

The Profile Panel shows a summary of all the time spent in the factorial method.The panel also shows both the inclusive and exclusive times (in addition to 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.

The *.trace file is constructed by a data file and a key file. The data file is used to hold the trace data. The key file provides a mapping from binary identifiers to thread and method names. If an older version of traceview is used, the key file and data file need to be combined into a trace file manually.

Figure 12.8 The TraceView analysis screen.

There is another way to generate a graphical call-stack diagram from trace log files in Android: dmtracedump.This tool requires the installation of the third-party Graphviz Dot utility to create the graphical output.

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook


  • Eve
    How to analyse the file .trace generated by traceview?
    7 years ago

Post a comment