Android Application Performance debugging

Source: Internet
Author: User

 

Overview

The key to creating a pleasant user experience is developing responsive applications. With the help of the components provided by the android * software development kit (SDK), the performance problem debugging task becomes easier due to the ease of use of performance analysis tools. In this article, we will understand different tools that can solve faults and debug performance problems or slightly improve the performance of completed applications. We will not go into details, but will only outline how to use these tools for your applications.

We will show these tools in eclipse. you can install the ADT plug-in according to the instructions in "ADT plugin for eclipse * (ADT plug-in for eclipse.

Ddms

Ddms is an application provided by Google *. It can be run as an independent tool or integrated into eclipse * Through the ADT eclipse * plug-in. It provides a powerful set of features to help you quickly understand the running status of applications.

Thread update

Thread Monitoring and Evaluation browsing in ddms is useful for applications that manage a large number of threads. To enable threads, click the update threads icon.

Figure 1

The following window displays the names and other details of all threads for the selected VM process.

Figure 2

Utime and stime represent the total time taken by threads to instantly run user code (utime) and system code (stime. The instantaneous time is defined by the system, but usually 10 ms. The asterisk indicates the daemon thread; the native state indicates that the thread is executing native code. Observe the sample data carefully. Obviously, it takes a lot of time to run GC in addition to the main thread of the application. A closer look at how the application processes object creation may help improve performance.

Heap Tool

View heap

Click Update heap to obtain heap allocation information for the selected virtual machine.

Figure 3

Figure 4

Click "cause GC" to start. The detailed information of the heap is displayed, and the allocation size graph for the specific allocation type is attached. If you have a allocation leak, this may be a good check point. by viewing the overall heap size trend, make sure it does not keep increasing during application running.

Allocation tracker (assign tracker)

The allocation tracker view displays deeper details about the allocation. Click Start tracking, perform an operation in the application, and then click get allocations )".

Figure 5

The list is sorted by allocation. The latest allocation is displayed first. Select it to see a stack trace for how to allocate it ).

Check the allocation details carefully. The following code looks like there is room for improvement:

dataStr += String.format(" Std. Dev.: %.3f, %.3f, %.3f\n", devX, devY, devZ);

In the preceding example, the code below can be reconstructed to save the overhead of constructing a temporary char ..

dataStrBuilder.append(String.format(" Std. Dev.: %.3f, %.3f, %.3f\n", devX, devY, devZ));

Method profiling)

Method profiling (method analysis) is a tool of ddms. It is very useful for quick overview of the time consumption distribution in an application. It can also be used for detailed viewing of time-critical functions.

Figure 6

When an application runs and executes an interesting task, if you want to obtain more performance data about the task, click Start method profiling )". Analyzer only collects a small amount of data (not seen for more than 2 or 3 seconds), so click the icon again in a few minutes to stop collection. Activating the method analyzer from ddms enables the tool to automatically use internal storage to store analysis results. After capturing is complete, they are sent back to the host for further analysis.

IDE automatically starts the traceview window to help you analyze the results in IDE (figure 7.

Figure 7

Parsing results are the most interesting part. Click "method call" in the bottom pane to create a hierarchical structure. The current method is displayed. First, the parent method of the method is called, and then the sub-method called from the selected method.

In this example, the onsensorchanged method is selected. When you register to receive notifications from sensor types, this method is called through the sensormanager API. The call method here is handlemessage, which comes from the operating system. Therefore, my implementation method is a good choice. Sub-methods are sorted by the percentage of time spent on the total. "Total" indicates the time spent in this method and all sub-methods called by this method. Therefore, for onsensorchanged calls, more than 70% of the time is spent on calcstandarddeviation and averagesamples. I expect that the call will take more time to calculate the standard deviation, rather than just average the sample. So with this new message, I can look into my implementation more deeply and discover code optimization points.

For details about traceview, see "profiling with traceview and dmtracedump )".

Analysis API

For more precise method analysis details, the call can be performed in the code to start and stop the analysis. You need to attach an SD card to your device to use this method. In the following example, we add a hook to better understand the sensor processing code:

-Collapse sourceview
Plaincopy to clipboardprint?
  1. Private Static Boolean doonce = true;
  2. @ Override
  3. Public void onsensorchanged (sensorevent event ){
  4. If (doonce ){
  5. Android. OS. Debug. startmethodtracing ();
  6. }
  7. Code under test...
  8. If (doonce ){
  9. Android. OS. Debug. stopmethodtracing ();
  10. Doonce = false;
  11. }
  12. }
[CPP]
View plaincopyprint?
  1. Private Static Boolean doonce = true;
  2. @ Override
  3. Public void onsensorchanged (sensorevent event ){
  4. If (doonce ){
  5. Android. OS. Debug. startmethodtracing ();
  6. }
  7. Code under test...
  8. If (doonce ){
  9. Android. OS. Debug. stopmethodtracing ();
  10. Doonce = false;
  11. }
  12. }

Private Static Boolean doonce = true; </P> <p> @ override <br/> Public void onsensorchanged (sensorevent event) {</P> <p> If (doonce) {<br/> android. OS. debug. startmethodtracing (); <br/>}</P> <p> code under test... </P> <p> If (doonce) {<br/> android. OS. debug. stopmethodtracing (); <br/> doonce = false; <br/>}< br/>The default trace file is/mnt/sdcard/dmtrace. Trace, which can be extracted from the device using the following command:

adb pull /mnt/sdcard/dmtrace.trace.

Run the independent traceview tool "traceview c: \ dmtrace. Trace" to open a user interface, similar to the user interface embedded in eclipse.

Layout User Interface tools

Layoutopt (layout selection)

Whenever an application is called, I want to gain a simple performance gain in the active user interface layout. Layoutopt analyzes your layout files and identifies potential performance problems. This issue has been discussed in this blog and reference file. Let's take a quick look at how to use this tool. Command Line usage:

layoutopt.bat C:\Projects\workspace\DeviceInformation\res

Note: I put the android * SDK tool directory in my path. It also looks like a tool that is available only when you describe the complete path of the directory in detail.

Output example:

C: \ projects \ workspace \ deviceinformation \ res \ drawable \ btn_icationication_ic_example.xml
C: \ projects \ workspace \ deviceinformation \ res \ drawable \ picture_frame.xml
C: \ projects \ workspace \ deviceinformation \ res \ Layout \ action_bar_custom.xml
23:23 this textview layout or its linearlayout parent is useless
C: \ projects \ workspace \ deviceinformation \ res \ Layout \ content_applicationinfo_main.xml
16: 19 This linearlayout layout or its linearlayout parent is useless
C: \ projects \ workspace \ deviceinformation \ res \ Layout \ content_benchmark_main.xml
C: \ projects \ workspace \ deviceinformation \ res \ Layout \ content_main.xml
C: \ projects \ workspace \ deviceinformation \ res \ Layout \ content_sensorinfo_main.xml
17: 20 This linearlayout layout or its linearlayout parent is useless

X: Y indicates the start and end rows marked in XML corresponding to the problem. The redundant layout mentioned above increases the overall loading time of the activity, which can be used to conveniently increase the loading speed of your activity.

Hierarchy Viewer)

Another useful tool for performance debugging is hierarchy viewer. This application can only be connected to the developer version of the android * operating system. Therefore, the simplest way to use this application is to use the simulator without the need to develop devices. Run the tool through the command line:

Hierarchyviewer

Conclusion

I hope that I have provided some new tools and knowledge for your application performance improvement. In addition to using these tools to find out what gains you can gain, many performance improvements can be achieved at the code level. Overview

The key to creating a pleasant user experience is developing responsive applications. With the help of the components provided by the android * software development kit (SDK), the performance problem debugging task becomes easier due to the ease of use of performance analysis tools. In this article, we will understand different tools that can solve faults and debug performance problems or slightly improve the performance of completed applications. We will not go into details, but will only outline how to use these tools for your applications.

We will show these tools in eclipse. you can install the ADT plug-in according to the instructions in "ADT plugin for eclipse * (ADT plug-in for eclipse.

Ddms

Ddms is an application provided by Google *. It can be run as an independent tool or integrated into eclipse * Through the ADT eclipse * plug-in. It provides a powerful set of features to help you quickly understand the running status of applications.

Thread update

Thread Monitoring and Evaluation browsing in ddms is useful for applications that manage a large number of threads. To enable threads, click the update threads icon.

Figure 1

The following window displays the names and other details of all threads for the selected VM process.

Figure 2

Utime and stime represent the total time taken by threads to instantly run user code (utime) and system code (stime. The instantaneous time is defined by the system, but usually 10 ms. The asterisk indicates the daemon thread; the native state indicates that the thread is executing native code. Observe the sample data carefully. Obviously, it takes a lot of time to run GC in addition to the main thread of the application. A closer look at how the application processes object creation may help improve performance.

Heap Tool

View heap

Click Update heap to obtain heap allocation information for the selected virtual machine.

Figure 3

Figure 4

Click "cause GC" to start. The detailed information of the heap is displayed, and the allocation size graph for the specific allocation type is attached. If you have a allocation leak, this may be a good check point. by viewing the overall heap size trend, make sure it does not keep increasing during application running.

Allocation tracker (assign tracker)

The allocation tracker view displays deeper details about the allocation. Click Start tracking, perform an operation in the application, and then click get allocations )".

Figure 5

The list is sorted by allocation. The latest allocation is displayed first. Select it to see a stack trace for how to allocate it ).

Check the allocation details carefully. The following code looks like there is room for improvement:

dataStr += String.format(" Std. Dev.: %.3f, %.3f, %.3f\n", devX, devY, devZ);

In the preceding example, the code below can be reconstructed to save the overhead of constructing a temporary char ..

dataStrBuilder.append(String.format(" Std. Dev.: %.3f, %.3f, %.3f\n", devX, devY, devZ));

Method profiling)

Method profiling (method analysis) is a tool of ddms. It is very useful for quick overview of the time consumption distribution in an application. It can also be used for detailed viewing of time-critical functions.

Figure 6

When an application runs and executes an interesting task, if you want to obtain more performance data about the task, click Start method profiling )". Analyzer only collects a small amount of data (not seen for more than 2 or 3 seconds), so click the icon again in a few minutes to stop collection. Activating the method analyzer from ddms enables the tool to automatically use internal storage to store analysis results. After capturing is complete, they are sent back to the host for further analysis.

IDE automatically starts the traceview window to help you analyze the results in IDE (figure 7.

Figure 7

Parsing results are the most interesting part. Click "method call" in the bottom pane to create a hierarchical structure. The current method is displayed. First, the parent method of the method is called, and then the sub-method called from the selected method.

In this example, the onsensorchanged method is selected. When you register to receive notifications from sensor types, this method is called through the sensormanager API. The call method here is handlemessage, which comes from the operating system. Therefore, my implementation method is a good choice. Sub-methods are sorted by the percentage of time spent on the total. "Total" indicates the time spent in this method and all sub-methods called by this method. Therefore, for onsensorchanged calls, more than 70% of the time is spent on calcstandarddeviation and averagesamples. I expect that the call will take more time to calculate the standard deviation, rather than just average the sample. So with this new message, I can look into my implementation more deeply and discover code optimization points.

For details about traceview, see "profiling with traceview and dmtracedump )".

Analysis API

For more precise method analysis details, the call can be performed in the code to start and stop the analysis. You need to attach an SD card to your device to use this method. In the following example, we add a hook to better understand the sensor processing code:

-Collapse sourceview
Plaincopy to clipboardprint?
  1. Private Static Boolean doonce = true;
  2. @ Override
  3. Public void onsensorchanged (sensorevent event ){
  4. If (doonce ){
  5. Android. OS. Debug. startmethodtracing ();
  6. }
  7. Code under test...
  8. If (doonce ){
  9. Android. OS. Debug. stopmethodtracing ();
  10. Doonce = false;
  11. }
  12. }
[CPP]
View plaincopyprint?
  1. Private Static Boolean doonce = true;
  2. @ Override
  3. Public void onsensorchanged (sensorevent event ){
  4. If (doonce ){
  5. Android. OS. Debug. startmethodtracing ();
  6. }
  7. Code under test...
  8. If (doonce ){
  9. Android. OS. Debug. stopmethodtracing ();
  10. Doonce = false;
  11. }
  12. }

Private Static Boolean doonce = true; </P> <p> @ override <br/> Public void onsensorchanged (sensorevent event) {</P> <p> If (doonce) {<br/> android. OS. debug. startmethodtracing (); <br/>}</P> <p> code under test... </P> <p> If (doonce) {<br/> android. OS. debug. stopmethodtracing (); <br/> doonce = false; <br/>}< br/>The default trace file is/mnt/sdcard/dmtrace. Trace, which can be extracted from the device using the following command:

adb pull /mnt/sdcard/dmtrace.trace.

Run the independent traceview tool "traceview c: \ dmtrace. Trace" to open a user interface, similar to the user interface embedded in eclipse.

Layout User Interface tools

Layoutopt (layout selection)

Whenever an application is called, I want to gain a simple performance gain in the active user interface layout. Layoutopt analyzes your layout files and identifies potential performance problems. This issue has been discussed in this blog and reference file. Let's take a quick look at how to use this tool. Command Line usage:

layoutopt.bat C:\Projects\workspace\DeviceInformation\res

Note: I put the android * SDK tool directory in my path. It also looks like a tool that is available only when you describe the complete path of the directory in detail.

Output example:

C: \ projects \ workspace \ deviceinformation \ res \ drawable \ btn_icationication_ic_example.xml
C: \ projects \ workspace \ deviceinformation \ res \ drawable \ picture_frame.xml
C: \ projects \ workspace \ deviceinformation \ res \ Layout \ action_bar_custom.xml
23:23 this textview layout or its linearlayout parent is useless
C: \ projects \ workspace \ deviceinformation \ res \ Layout \ content_applicationinfo_main.xml
16: 19 This linearlayout layout or its linearlayout parent is useless
C: \ projects \ workspace \ deviceinformation \ res \ Layout \ content_benchmark_main.xml
C: \ projects \ workspace \ deviceinformation \ res \ Layout \ content_main.xml
C: \ projects \ workspace \ deviceinformation \ res \ Layout \ content_sensorinfo_main.xml
17: 20 This linearlayout layout or its linearlayout parent is useless

X: Y indicates the start and end rows marked in XML corresponding to the problem. The redundant layout mentioned above increases the overall loading time of the activity, which can be used to conveniently increase the loading speed of your activity.

Hierarchy Viewer)

Another useful tool for performance debugging is hierarchy viewer. This application can only be connected to the developer version of the android * operating system. Therefore, the simplest way to use this application is to use the simulator without the need to develop devices. Run the tool through the command line:

Hierarchyviewer

Conclusion

I hope that I have provided some new tools and knowledge for your application performance improvement. In addition to using these tools to find out what gains you can gain, many performance improvements can be achieved at the code level.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.