- Android application (performance and more) analysis tools — Tutorial
- 1. Overview
- 2. Android Basics
- 3. StrictMode
- 4. Developer Settings
- 5. Traceview
- 5.1. Introduction
- 5.2. Using Traceview from Android Studio
- 5.3. Using TraceView from the command line
- 6. Exercise: Traceview
- 6.1. Create example project
- 6.2. Perform trace
- 6.3. Solve performance problems
- 7. Hierarchy Viewer
- 8. Exercise: Hierarchy Viewer
- 9. Layout optimization
- 10. Memory Dumps
- 11. Systrace
- 12. Simulating pixel density
- 13. Android templates
- 14. Profile GPU rendering
- 15. Analyzing Overdraw
- 16. Useful open source tools for application analysis
- 16.1. Using Leak Canary to find memory leaks
- 16.2. Using AndroidDevMetrics to display performance data
- Ensure High-Quality Android Code With Static Analysis Tools
- What Are Static Code Analysis Tools?
- Benefits
- Setup
- Checkstyle
- Integrating Checkstyle
- FindBugs
- Android Lint
- Bonus: StrictMode
- Conclusion
Android application (performance and more) analysis tools — Tutorial
Analyzing Android performance issues. This tutorial describes the available tools in Android to perform a performance analysis of Android applications.
1. Overview
It is very important for Android applications to perform all operations as fast as possible. This description lists the available tools to trace and optimize your Android application.
2. Android Basics
The following assumes that you have already basic knowledge in Android development. Please check the https://www.vogella.com/tutorials/Android/article.html — Android development tutorial to learn the basics.
3. StrictMode
You should avoid performing long running operations on the UI thread. This includes file and network access.
To ensure this you can use StrictMode. StrictMode is available as of API 9 (Android 2.3.3) and allows to setup thread policies for your application.
Via StrictMode you can instruct the Android system to crash your application if it performs long running operations, e.g. I/O in the user interface thread.
The following code shows how to use StrictMode. As the activity violates these settings it will crash.
StrictMode should only be used during development and not in your live application.
4. Developer Settings
Developer Settings in your Setting application of your Android phone allow to set configurations which simplify the analysis of your application. For example you can enable that the touch area is highlighted.
If you have a phone with Android 4.2, try going to the About section in the Settings and tap the Build number entry 7 times.
If you phone does not have this option try using an emulator.
In some cases you need to restart the application to make the setting work.
5. Traceview
5.1. Introduction
Traceview is a graphical viewer to see logs created by an Android application. Via Traceview you can measure the performance of your application to identify performance problems.
Traceview is located as standalone tool in the tools folder of your Android SDK installation folder and it also integrated into Eclipse via the Android Developer Tools (ADT).
5.2. Using Traceview from Android Studio
Android Studio supports tracing via the Android Device Monitor. Use Tools Android Android Device Monitor to open it.
To start tracing an application select your application process in the Devices view and select the Start Method Profiling button as depicted in the following screenshot.
Use your application and re-press the same button to stop profiling. This will open a new editor which shows you the tracing results.
You can zoom into the graphic to get more details. To zoom out, double-click on the time line.
5.3. Using TraceView from the command line
To start tracing some code put the following code snippet around it.
The parameter «yourstring» tells the system that it should store the data under «/sdcard/yourstring.trace». To save data on the sdcard your application needs the WRITE_EXTERNAL_STORAGE permission. After running your application you can copy the results from the device via the adb command line tool.
This will start Traceview which allow you to analyze your performance data via a graphical way. The DDMS view has also a trace button available. This will trace the running application and does not require an additional authorization.
6. Exercise: Traceview
6.1. Create example project
Create an Android application with the top level package called com.vogella.android.traceview .
Add the following key to your values/strings.xml file.
Create the following layout file called rowlayout.xml.
Implement the following adapter for your ListView .
Implement a ListView in the activity which shows 1000 random generated strings.
Add the permission to write on external storage to your application.
6.2. Perform trace
Run your application. Afterwards connect to your device via adb and copy the resulting performance trace to your drive. Analyze the result.
6.3. Solve performance problems
Improve the performance based on the measurements of Traceview. Here are some pointers what to improve.
Replace the bold styling in the getView() method with the corresponding android:textStyle=»bold attribute in your layout to avoid the Html.fromHtml() call.
Move the sorting to another place
Reuse convertView in your Adapter, if not null
Avoid the findViewById() method call by using the HolderPattern in your ListView
The layout is also not optimized. We reuse this exercise in the exercise for the HierarchyView |
7. Hierarchy Viewer
The Hierarchy View perspective allows you to visualize the View hierarchy of your Android application and helps you to identify unnecessary layers in this hierarchy.
You can open the Hierarchy View via Window Open Perspective Other… Hierarchy View
In the Windows view you can select the process for which you want to analyze the view hierarchy.
The currently active layout is analyzed and displayed.
The Tree View view show three light bulbs for the views. The first one indicates the time for calculating the size of the view, the second one indicates the time for creating the layout and the third one for drawing the view. Performance intensive work is indicated with the yellow or red color.
8. Exercise: Hierarchy Viewer
Continue to use the same example as in the Traceview Excercise. Open the Hierarchy View perspective and analyze the view layer.
While none of these layout layers is very performance intensive it contains redundant layers and unnecessary views. Remove the unnecessary view layers.
9. Layout optimization
You should optimize your layout. The following gives an example how to use FrameLayout with an ImageView and one TextView to create result despicted in the next screenshot. It uses the possibility to position the TextView in the layout and to style parts of its text content differently.
Create a project called com.vogella.android.textview.spannablestring.
Add two news styles to your styles.xml file.
Create the following layout.
Style your text in the TextView via separate TextAppearanceSpan as demonstrated in the following code.
The resulting layout is much faster than a layout based on RelativeLayout . Also HTML styling was avoid as the HTML parser is relatively expensive.
10. Memory Dumps
You can create a memory snapshot and analyze it with the https://www.vogella.com/tutorials/EclipseMemoryAnalyzer/article.html — Eclipse Memory Analyzer.
11. Systrace
Systrace allows to measure the performance directly at kernel level. To enable systrace, select the Developer options and select the Enable traces entry. In the next dialog you can define what type of events should be profiled, e.g. Graphics and View.
To use systrace, open a terminal and run systrace.py from the android_sdk_installdir/tools/systrace directory. You may have to set it to executable (chmod a+x systrace.py on Linux).
You can also start Systrace directly from Eclipse via the DDMS perspective.
Systrace captures events for 5 seconds. As result Systrace creates a HTML file which allows you to analyze potential issues.
12. Simulating pixel density
You can use the command line to simulate different device densities and display resolutions.
This allows you to use a device with a high device density and resolution to simulate other devices.
13. Android templates
You can define your own templates for the Android project creation wizard. See the following links for more information.
14. Profile GPU rendering
In the Developer options in the Setting or your Android device you can activate Profile GPU rendering. With this option the system tracks the time it took to draw the last 128 frames.
After activating this and restarting your application you can get the information via the following command.
To measure the frame rate of your application, e.g. by scrolling on a list view. In most cases you need to interact with your application to trigger that it re-draws itself.
In the resulting log, look for a section called Profile data in ms.
15. Analyzing Overdraw
Overdraw happens if you draw something on top of something else. For example an activity has a Window as background. If a TextView it added to the application, the TextView is drawn on top of the Window .
Overdrawn is therefore intended to happen. But unnecessary overdraw should be avoid to have the best performance possible.
Unnecessary overdraw can be caused by complex view hierarchies. In general a 2x overdraw (a pixel gets drawn three times) is standard and expected but more should be avoided.
You can enable a visualization of overdraw via the Development Settings with the Show GPU overdraw setting. This section adds colors to your screen based on the number of overdraws. The following table explains the used color schema.
Color | Meaning |
---|---|