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).
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:
If you have all 3 completed already, you can skip ahead to the DDMS Method Profiling.
You can download Android Studio + Android SDK, or download just the Android SDK to be used with command line or Eclipse.
Follow these steps to enable developer options on your android device. This is not necessary if you plan to just use emulators.
From the Command Line:
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.
Note it might take some time for the data to be returned to DDMS depending on how much data was collected.
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.
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 ?