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:
- Download & Install the Android SDK. This includes DDMS.
- Enable Developer Options on your test device (unless you will use emulators)
- 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
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.
- Open device settings
- Open "About phone"
- Click on "Build number" 7 times, you should see a toast indicating "You are now a developer"
- Now in device settings you should see the option "Developer options", click dev options
- Enable "USB debugging", you're now ready to deploy apps and profile apps
How to Launch DDMS...
From the Command Line:
- Navigate to your android sdk root folder (i.e. "C:\Program Files\Android\sdk")
- Navigate to \tools
- Run monitor.bat
- Install Eclipse Android Developer Tools plugin (ADT) http://developer.android.com/tools/sdk/eclipse-adt.html
- Windows -> Open Perspective -> DDMS
- Tools -> Android -> Android 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.
- Deploy your test app to your test device
- In DDMS under "Devices" on the far left you should see your test device with your test app listed under it
- Click your test app
- Click the "Start Method Profiling" button
- 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.
- Let's choose Trace based profiling if prompted.
- Perform an action inside the test app (i.e. scroll, click, possible performance sucking culprit)
- Click the "Stop Method Profiling" button
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. 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. 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. 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". 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.
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 ?