{"id":47,"date":"2014-01-06T13:22:00","date_gmt":"2014-01-06T13:22:00","guid":{"rendered":"https:\/\/cindypotvin.com\/?p=47"},"modified":"2017-02-18T16:32:12","modified_gmt":"2017-02-18T21:32:12","slug":"using-android-traceview-in-eclipse","status":"publish","type":"post","link":"https:\/\/cindypotvin.com\/using-android-traceview-in-eclipse\/","title":{"rendered":"Using Android Traceview in Eclipse"},"content":{"rendered":"

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 plugin, but you can also use the traceview command.<\/p>\n

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.<\/p>\n

Profiling from the code<\/h2>\n

Profiling for the code allows you to analyse a section which you already suspect is problematic. To profile from the code :<\/p>\n

    \n
  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 :\n
    Debug.startMethodTracing(\"logname\");<\/pre>\n

    And to stop profiling, add the following call :<\/p>\n

    Debug.stopMethodTracing();<\/pre>\n

    Those calls don\u2019t need to be in the same method, but make sure the stopMethodTracing<\/em> 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<\/em>.<\/li>\n

  2. Start the application and execute the problematic operation to trace it.\u00a0 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.<\/li>\n
  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<\/em> section below to check the result.<\/li>\n<\/ol>\n

    Profiling from Eclipse<\/h2>\n

    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.<\/p>\n

      \n
        \n
      1. In the DDMS perspective, click the Start Method Profiling<\/em> button.<\/li>\n<\/ol>\n<\/ol>\n

        \"SNAGHTML69d982c\"<\/p>\n