TraceView is a visual debugging tool for Android. With it, you can see exactly how your code performs at run time. It can help you better understand the efficiency of the code running process, and then improve the code to improve the experience of your application.
Before using the TraceView tool, you need to make a TraceView log file that contains information about the app's tracking, and then use the TraceView tool to analyze the file.
Two ways to generate trace logs
Code mode
Use the Debug class in your code to follow the build log file, call the Startmethodtracing () method to start tracing, and call the Stopmethodtracing () method to end the trace. The benefit of generating logs with code is that you can control the scope of the trace very precisely because you can set the location of the start and end traces yourself.
To generate files in this way, be aware of:
You need to add the following permissions read_external_storage in the Androidmanifest file to allow your app to write to the external storage space.
The following is an example of using the Debug class to generate the log:
Start tracing to "/sdcard/calc.trace" debug.startmethodtracing ("Calc"); // ... Stop tracing debug.stopmethodtracing ();
For example, you can call Startmethodtracing () in OnCreate () to start the trace, and use Stopmethodtracing () in the OnDestroy () method to end the trace.
Call Startmethodtracing ("Calc"), the system will specify the name of the trace file as Calc. If you use Startmethodtracing (), the system will trace the default name of the log file to Trace-base-name.trace. When you call the Startmethodtracing () method, the system begins to cache the data and write to the log file, Until you call stopmethodtracing. If the system reaches the cache size before the end tracking method is called, the log trace is actively ended and notifications are sent to the notification bar.
After you create the log file, you need to copy the log files from your phone to your computer. You can use the following command to copy the operation:
ADB pull/sdcard/calc.trace/tmp
To view the trace log file on TraceView
Use the TraceView <trace-base-name> Format command to view the log file as follows:
Traceview/tmp/calc
Use Trace directly
First select the process you want to track the app in Ddms's devices panel, then click on the icon shown in the image below.
After clicking on the icon, the following dialog box pops up
The TraceView provides two types of log trace modes:
A. Tracking type. keeps track of the entire process of each method from the inside out. This approach provides accurate execution information, but the resources used to occupy the machine are large.
B. Sample type. data is collected on a per-frequency basis through a virtual machine. This approach consumes less resources than the previous one, but requires longer execution time to obtain representative data. At the same time, you can customize the frequency of data collection.
Click OK, after running your app for a while, then click on the Devices Panel button to end the data acquisition, wait a few seconds, TraceView will automatically display the collected data.
Understand the specific use of TraceView
TraceView can present the collected data information as a view. The view of TraceView consists of two parts:
-Timeline panel. provides information about a thread and method from the beginning to the end of the time.
-profile Panel. provides information about the execution of a method.
Time Axis Panel
is an example of the timeline panel. The left column represents a different thread, and each thread occupies one line, and the first line in the example [1]main] is the main thread. In the right column is a timeline, and time increments from left to right, and you can clearly see from the table how many different threads are performing at different time periods. Specific to each thread, you can see how long it takes to perform different methods, and different methods are replaced with different colors (supplemented by the use of alternating samsara instead of different methods). A thin line appears underneath the drawing block, and the part that is selected by the thin line is the time that the method you have selected executes.
Tips,
When using TraceView, to zoom in on a part of the color you are interested in, use the mouse to select the color block you are interested in is OK.
However, TraceView does not support the direct reduction of the panel's timeline, you can use the following two ways to reduce the timeline.
A. Click on the upper left corner of the timeline panel and the timeline will revert to the first global appearance.
B. Zoom in to the upper-right corner of the timeline panel, the mouse will change, then stretch the mouse to the left, and you can see more information in the timeline.
Information Panel
The profile panel contains information about the methods that are executed in the time you are tracking, and the main features are shown in two ways:
A. The information that a method executes , including
Incl CPU time a method consumes the CPU
EXCL CPU time a method itself (excluding child methods) takes up the CPU
incl Real time how long a method actually executes
Excel real Time a method itself (excluding child methods) actually executes
Number of Calls+recurcalls calls + recursive callbacks
Cpu Time/call
Real Time/call
B. Click on a method on the left to see more information about the parent method and all child methods, where the parent method is in the same row as the purple background, and the child method is in the same row as the yellow background.
By clicking the name of a column in the right column, you can have the table globally arranged in the order of the data size of the column. With data presentation, you can easily find ways to reduce the application performance portion of your doubts.
For example, in the [Information Panel], you can take the incl Cpu time the longest way to start, that method is not the number of calls, is not the normal situation. If a method takes up CPU for a long time, but the number of calls is not many, this situation is more doubtful, combined with the specific method to view, is not well-written code is not good enough.
TraceView provides information about the amount of CPU time, the number of calls that are consumed by different methods in the code execution process in different threads. It can help you judge the performance of your code, and you can further improve your code to improve your application's performance and user experience.
Use TraceView to debug and improve Android app performance