In my previous blog post, I Want to Profile My Android Apps! What Tools are Available?, I covered what profiling was, why you need it, and what tools are available when profiling android apps. In this blog post I will dive deeper into how to perform Android method profiling to profile my app's method execution time using DDMS (Dalvik Debug Monitor Server).

Android Profiling Set-up Guide

Before I dive into method profiling with DDMS, let me provide a step-by-step guide to get your environment set up for profiling. In order to follow along with this post and my next post you will need to:

  1. Download & Install the Android SDK. This includes DDMS.
  2. Enable Developer Options on your test device (unless you will use emulators)
  3. Launch DDMS from either the command line, Eclipse, or Android Studio

If you have all 3 completed already, you can skip ahead to the DDMS Method Profiling.

Download & Install the Android SDK

You can download Android Studio + Android SDK, or download just the Android SDK to be used with command line or Eclipse.

Enable Developer Options on the Device

Follow these steps to enable developer options on your android device. This is not necessary if you plan to just use emulators.

  1. Open device settings
  2. Open "About phone"
  3. Click on "Build number" 7 times, you should see a toast indicating "You are now a developer"
  4. Now in device settings you should see the option "Developer options", click dev options
  5. Enable "USB debugging", you're now ready to deploy apps and profile apps

How to Launch DDMS...

From the Command Line:

  1. Navigate to your android sdk root folder (i.e. "C:\Program Files\Android\sdk")
  2. Navigate to \tools
  3. Run monitor.bat

From Eclipse:

  1. Install Eclipse Android Developer Tools plugin (ADT) http://developer.android.com/tools/sdk/eclipse-adt.html
  2. Windows -> Open Perspective -> DDMS

Android_Eclipse_DDMS From Android Studio:

  1. Tools -> Android -> Android Device Monitor

Android_Studio_Device_Monitor

DDMS Method Profiling

The following 8 steps demonstrate how to start and stop a typical method profiling process. This will gather the data that we will analyze next.

  1. Deploy your test app to your test device
  2. In DDMS under "Devices" on the far left you should see your test device with your test app listed under it
  3. Click your test app
  4. Click the "Start Method Profiling" button
  5. Now, if your test device is Android 4.4 or later you'll be prompted to choose trace-based or sample-based profiling, if earlier than Android 4.4 only trace-based profiling is available. Sample based profiling traces a method at specified time intervals, with overhead proportional to frequency. Basically allows you to profile your app without any visual lag (if time interval is low enough). Trace based profiling traces every method entry and exit point, high overhead. Basically you will see visible lag in your app as you scroll or perform animations, but all the data is collected.
  6. Let's choose Trace based profiling if prompted.
  7. Perform an action inside the test app (i.e. scroll, click, possible performance sucking culprit)
  8. Click the "Stop Method Profiling" button

Note it might take some time for the data to be returned to DDMS depending on how much data was collected. Android_DDMS_Method_Profiling

Analyzing Results with the Timeline Panel

After you've collected method profiling data you can analyze it to find problems and measure the health and speed of each method call using the timeline panel. The Timeline Panel is a visual representation of the collected data over time. You can zoom in and click on method calls to bring up the granular data in the Traceview panel. Below is an example of profiling for 5 seconds. As you can see the initial timeline panel is not all that useful. Android_Method_Profiling1 You can zoom in on the timeline panel by clicking, dragging, and releasing the mouse between time intervals. Below I've zoomed in on the last second of collected data. It's still not all that useful. Android_Method_Profiling2 Next let's zoom in even further and view data collected during the last 20 milliseconds. Now we can visually see how long different method calls take. By moving my cursor to the different color blocks I can see the method call name. Note that the size of the block indicates the relative time taken to execute. Android_Method_Profiling3 In the sample above I can visibly see that the dark green blocks and the pink blocks are my slowest methods. By clicking on these color coded blocks it will bring up that method in the Traceview panel explained below.

Analyzing Results with the Traceview Panel

The Traceview panel is my preferred way to find the performance issues in my app. By default the list of method calls will be ordered by the "Incl Cpu Time %" column. "Incl Cpu Time" indicates the percentage that method took overall to execute. Additionally the number at the beginning of the name column will tell you the order of method calls in descending order by execution time. Scroll down through the list until you find a class with a package name from your app, i.e. "com.example.chrisr.myapplication". Android_DDMS_Traceview_Panel By clicking on the method name it will expand the tree so that I can see all the parent (methods that called this method) and children (methods this method calls). If I look at the children I can determine which child method is slowing this method down the most. You can dig deeper by clicking on children to navigate to that child's overall place in the outer tree. Record the Cpu Time %, make changes to the method, and compare the Cpu Time %. Comparing the Cpu Time % each time the method changes will help you actually determine if your improving the performance.

What's Next

So far we looked at step-by-step how to enable method profiling and execute a typical scenario using DDMS. In my next blog post I will cover another important piece of performance profiling: memory tracking. Continue reading Tracking Memory Allocation with DDMS and Android Studio >> << Go back I Want to Profile My Android Apps What Tools are Available ?