TraceView for Android performance analysis tools
TraceView is a good performance analysis tool on the Android platform. It allows us to understand the performance of the program we want to track in a graphical way, and can be specific to the method.
TraceView Overview
TraceView is a unique data collection and analysis tool on the Android platform. It is mainly used to analyze hotspot of Android applications. TraceView itself is only a data analysis tool, and data collection requires the use of the Debug class in the Android SDK or the use of the DDMS tool. The two are used as follows:
- Before starting some key code segments, the developer calls the startMethodTracing function of the Debug class in the Android SDK, and calls the stopMethodTracing function before the end of the key code segment. During the operation of these two functions, the execution status of all the threads of the application during the running time (Note: Only Java threads can be used) will be collected, save the collected data to a file under/mnt/sdcard. The developer then needs to use the TraceView tool in the SDK to analyze the data.
- Use the DDMS tool in the Android SDK. DDMS can collect the function call information of a running process in the system. For developers, this method is applicable when the source code of the target application is not available.
In DDMS, TraceView is used as follows. debugging personnel can select an application in Devices and click Start Method Profiling (enable Method Analysis) and StopMethod Profiling (stop Method analysis)
After method analysis is enabled, test the target page of the application. After the test is completed, stop the method analysis. The trace Analysis page of DDMS is displayed, as shown in:
The TraceView interface is complex. Its UI is divided into two upper and lower panels: Timeline Panel and Profile Panel ). The upper part of the Timeline Panel is Timeline Panel, which can be subdivided into two Pane:
- Pane on the left shows the thread information collected in the test data. The figure shows that the test data collects information about the main thread, sensor thread, and other system auxiliary threads.
- The Pane on the right shows the time series. The time line shows the information about function calls involved in each thread test period. This information includes the function name and function execution time. As shown in the figure, the content of the corresponding lines of Thread-1412 threads is very rich, and the work of other threads in this period is much less.
- In addition, developers can move the timeline vertical axis in the timeline Pane. On the top of the vertical axis, information about the functions being executed by a thread in the current time point is displayed.
The lower part is divided into ProfilePanel (analysis Panel). Profile Panel is the core interface of TraceView, which has rich connotation. It mainly displays the information about each function call in a thread (select a thread in the Timeline Panel first), including the CPU usage time and number of calls. The information is the key basis for finding the hotspot. Therefore, developers must understand the meaning of each column in the Profile Panel. The following table lists important column names and their descriptions in Profile Panel.
Practical Analysis:
First, find the class you have written. Here we use AddGoodsMatchActivity written by myself as an example. We can know that the onCreate method occupies 314.069 ms, and the time used by each method in the onCreate method is also clear. Focus on the Analysis of Its setContentView method, because it occupies 266.874 ms, accounting for 82.5% of the total onCreate method, click setContentView Method
Click SetContentView.
Click inflate.
From this we can see the time and number of calls of our own method, so as to analyze the problem.