Click here to Skip to main content
15,867,686 members
Articles / Mobile Apps / Android

Using Android Traceview in Eclipse

Rate me:
Please Sign up or sign in to vote.
4.69/5 (6 votes)
14 Jan 2014CPOL3 min read 14.9K   9   2
How to use Android Traceview in Eclipse

The best way to solve a performance problem with an Android application is to profile the application by tracing the execution. This way, you can make decisions on what to improve based on real data and detect bottlenecks in the application. In the Android SDK, the Debug object handles the profiling, and the TraceView utility is used to view the generated trace file. I will describe how to profile using the tools included in the Eclipse Android SDK plug in, but you can also use the traceview command.

Keep in mind that a .trace file can get pretty big, so be careful to trace only the parts of the application that are problematic. If you have no idea what to profile, you can trace at large the first time and drill down to specific sections later on. Also, execution is slowed down when methods are traced, so you should only compare traced execution time with another traced time.

Profiling from the Code

Profiling for the code allows you to analyze a section which you already suspect is problematic. To profile from the code:

  1. Add calls to the Debug class methods where you want to start and stop profiling. To start profiling in any method, add the following method calls:
    Java
    Debug.startMethodTracing("logname");

    And to stop profiling, add the following call:

    Java
    Debug.stopMethodTracing();

    Those calls don’t need to be in the same method, but make sure the stopMethodTracing will be reached. Also, your application must have permission to write to the SD card, or else you will get an exception Unable to open trace file “/sdcard/logcat.trace” : Permission denied.

  2. Start the application and execute the problematic operation to trace it.  A message is displayed in the Logcat output when the profiling starts and stops, and a logname.trace file will be created at the root of the file system of the device.
  3. Navigate to the root of the SD card of the device, and copy the .trace file to the desktop. Open the .trace file with eclipse and it will be shown in the trace tool. See the Analyzing the .trace file section below to check the result.

Profiling from Eclipse

Profiling during debugging can allow you to pinpoint a problem appearing at random times during the execution or for which you are unsure which lines of code are causing the problem.

  1. In the DDMS perspective, click the Start Method Profiling button.

    SNAGHTML69d982c

  2. Press the same button again (Stop Method Profiling) to stop profiling. The result will appear immediately in Eclipse.

Analyzing the .trace File

When opening a .trace file, the following window is shown:

image

At the top, each thread has its own timeline. In this application, there is only a main thread, the other threads are system thread. In my opinion, the most interesting columns are:

  • Incl CPU %: Percent of the total CPU time used by this method, including the execution time of all the child methods.
  • Incl CPU Time: CPU time in milliseconds used by this method, including the execution time of all the child methods.
  • Calls + RecurCall/Total: The number of calls to the child method by the parent method (and recursive calls) /Total calls to the method for the whole .trace file­.

You can drill down on any method to see the child methods and analyze them. In that case, drilling down on the generateRandomNumber method shows that the child method saveRandomNumber makes up the bulk of the execution time. In fact, if you look at the Calls column, you can see that it was called 1000000 times. The method was called multiple time for the test, but for a real profiling case, this is where you should have paid attention: the number of calls may be reduced, or the execution of the method optimized.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Canada Canada
Cindy Potvin is a software developer based in the Montreal area. At her day job, she creates web applications using the ASP.NET MVC framework and mobile applications using the Android SDK.

Comments and Discussions

 
Questionwhat are the meaning of other columns ? Pin
vrnithinkuamr28-Aug-14 1:55
vrnithinkuamr28-Aug-14 1:55 
SuggestionDDMS Pin
Leye013-Mar-14 15:33
Leye013-Mar-14 15:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.