Profiling Android Applications

From JPCT
Revision as of 20:57, 6 August 2010 by Raft (Talk | contribs)

Jump to: navigation, search

Profiling Android Applications

Profiling is important since it allows you to analyze runtime behaviour of your application. You can use gathered information to spot points which need improvement and hence optimize your application. Luckily Android provides tools for profiling if you had an Android device.

Profiling within Eclipse

Seems as this option is available for Linux users. Windows users report they don't have this option. I don't know about Mac. You need to install Android ADT plugin. See: insallation page. You also need your device connected to your computer and debuggable flag is set in AndroidManifest file.

In Eclipse, make sure Devices view is visible. Select Window|Show View|Other|Android|Devices. In Devices view, on upper right, among other buttons you will see "Start Method Profiling" button. Select your application among list and click "Start Method Profiling" button. The button's appearance and text changes. Use your application for some time and click button again to finish profiling. ADT plugin automatically pulls trace file from device and starts Traceview application to display results.

Eclipse ADT Devices view.png

Profiling without Eclipse

You can also profile your application without Eclipse. Android command line tools are enough for this. All these commands are in tools folder in Android SDK.

Make sure your device is connected to your computer and your application is running and debuggable flag is set in AndroidManifest file..

The command to start profiling is:

adb shell am profile <PROCESS_ID> start <TRACE_FILE>

You can determine the PROCESS_ID either from the list within Eclipse or via the following command:

adb shell ps

TRACE_FILE is the file you want to store your trace results. Typically a file in the sdcard. For example:

adb shell am profile 5397 start /sdcard/my.app.trace

After started profiling, run your application for sometime. Then to stop profiling:

adb shell am profile <PROCESS_ID> stop

Now the profiling is completed and results are stored in the file. To see the results you need to pull trace file from your device to your computer:

adb pull <TRACE_FILE> <FOLDER or FILE_NAME IN COMPUTER>

For example:

adb pull /sdcard/my.app.trace ./

The latest step is to open the Traceview application to see the results.

traceview <LOCAL_TRACE_FILE>

Continuing our example:

traceview ./my.app.trace

That's it. Enjoy your results.

Inspecting results

Traceview application is pretty straightforward to use. So no need to go in to details here. Also have a look at traceview page

Troubleshooting

Unable to open trace file '...': Permission denied

This sometimes occurs, I don't know why. Add the following uses-permission element to your AndroidManifest.

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Also see this post.

java.lang.SecurityException: Process not debuggable: ProcessRecord...

Make sure you have set debuggable flag in your AndroidManifest file.

    <application .. android:debuggable="true">
       ...
    </application>
My application is not listed in Devices view

Make sure you have set debuggable flag in your AndroidManifest file.

    <application .. android:debuggable="true">
       ...
    </application>