Profiling Android Applications

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

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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.

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.

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

Profiling within Eclipse worked out of the box on my machine for both G1 and Nexus One. But after some time, it begun to fail for Nexus One complaining about it can't create file in sdcard. After some googling, I found this post. It says writing to sdcard requires android.permission.WRITE_EXTERNAL_STORAGE permission, adding that fixed the issue. But I don't know why it worked in the first place.