TraceView of Android Performance Optimization Tool

TraceView of Android Performance Optimization Tool

Introduction

  • TraceView is a data collection and analysis tool unique to the Android platform. This tool can let us understand the performance of the program that needs to be photographed in a graphical way. It can be specific to the method, which is mainly used to analyze the Hotspot of the application in Android. The tool itself is a data analysis tool. How to obtain data and analyze it? The author uses a demo.apk written before to illustrate the analysis.

Data collection

Collecting data is to generate the trace file we need to analyze. There are three ways to generate this file.

  • use code
  • Using Android Studio
  • Use DDMS tools
Code way to generate trace file

The use of code to generate traces files mainly uses the Debug API of the Android system. This class is under the android.os package. The sample code is as follows

//开始 trace,默认保存文件到 "/sdcard/filename.trace"
Debug.startMethodTracing(String fileName);
/*需要分析的代码..*/
Debug.stopMethodTracing(); 

To briefly explain, when the start trace code is called, the system will generate a trace file and trace data, and when the call ends the trace code, the trace data will be written to the trace file. Since the file is saved in the root directory of the sdcard, we can copy it out or pull it out through adb

Use Android studio to generate trace files
  • Use the Android monitor that comes with Android studio to generate trace, as shown in the figure below, when the debug application is started, the Start Method Tracing button will be displayed in a clickable state,Start trace

  • After clicking, it will start to trace the trace. If you want to end the trace, click again, and Android studio will automatically open it for you, as shown in the following figure

    Trace analysis graph

    As can be seen from the above, the execution time of some methods, the number of invocations and their invocation relationships can also be searched to filter specific content.

    Different threads can be switched from the upper left corner. When the mouse hover state is placed on a method block, the start time and end time of the method can be displayed, as well as some time ratios, as shown in the following figure

    Detailed description

Generate traces using DDMS
  • DDMS is an Android debugging monitoring tool. It provides us with screenshots, logs, view levels, and memory usage. This function is integrated in Android Studio or eclipse. If you configure Android environment variables, you can Type DDMS on the command line, the tool is under \android_sdk\tools. In the Devices list, select your device and process, perform trace, click again to stop the trace, after the trace is stopped, DDMS will automatically load the trace file. The main functions of the panel are described below, as shown in the following figure.

    DDMS Trace

  • Some important items are mainly marked in the figure, and the execution time of each thread can be clearly seen with DDMS.

    detailed

  • Finally, look at the data analysis panel. In the data analysis panel, you can click on a function to expand more detailed information. After expansion, most of them have the following two categories:

    • Parents: the parent class method that calls this method

    • Children: the subclass method called by this method

    • If the method contains recursive calls, there may be two more categories:

    • Parents while recursive: The parent class method involved in recursive calls

    • Children while recursive: Subclass methods involved in recursive calls

    • As for the red box of the data analysis panel, the meaning of the main fields has been marked, click an entry to sort

      Analysis list

  • **Main:** Very important indicator: Calls + RecurCalls/Total , most important indicator: Cpu Time/Call

    Because we are most concerned about two points, one is a function that is called a few times, but each call takes a long time. This can be reflected from Cpu Time/Call . The other one is those functions that do not take long by themselves, but are called very frequently. This can be reflected from Calls + RecurCalls/Total .

Epilogue

  • Finally, let me explain that in practice, the analysis is more difficult, but when the experience is stuck, we can use TraceView to locate the problem. The next article summarizes the use of the Systrace tool.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325545188&siteId=291194637