Android performance optimization is a common problem in Android development. Next, we will give a brief summary of the knowledge points of Android performance optimization, which will be summarized in terms of tools and code. For better work, you must first sharpen the tool. This article first looks at Android performance optimization tools.
TraceViewtraceview is a self-contained tool in the Android SDK. It can perform statistical analysis on the time consumption of method calls in applications. It is an important tool for Android performance optimization and analysis. Traceview is located in the tools directory under the SDK. You can run the traceview command in the cmd window or in ddms when using traceview. I personally prefer the latter method, it is easy to operate and does not need to modify the code.
Method 1: Call the startMethodTracing and stopMethodTracing methods at the start and end of the traceview analysis. The following sample code is shown:
public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Debug.startMethodTracing();}@Overrideprotected void onDestroy() {super.onDestroy();Debug.stopMethodTracing();}}
The ". trace" file is generated after the end, and the data file is analyzed by running the traceview *. trace command. StartMethodTracing has six methods that can be called as needed. If the ". by default, the trace file will be stored in the root directory of the external storage (/sdcard/), named "dmtrace. trace ", we recommend that you customize the trace name (startMethodTracing (String traceName) for convenience )). Method 2: traceview can also be used directly in ddms, that is, after selecting a process to be monitored in ddms, click the small icon to start monitoring, and click the small icon again at the end of monitoring, ddms automatically opens the traceview view: examples/outputs + ax49GhtcTSu7j2o6nJ + rPJtcShowo8aW1nIHNyYz0 = "http://www.2cto.com/uploadfile/Collfiles/20140509/2014050909131720.jpg" alt = "\"> we can see that the entire traceview is divided into two parts, the upper part
Timeline Panel)The lower half is called
Profile Panel).
Timeline panel
The ruler at the top of the interface represents the time period of MethodTracing (from Debug. startMethodTracing () to Debug. stopMethodTracing ). The function execution time graph of each thread is on the right of the same line with the thread name. NOTE 1: The line width indicates the time used to execute the function itself. Note 2: The subfunction time series called by the function are mixed between the time series used for the operation of the function. Note 3: The height of the timeline does not have any significance. Note 4: functions are nested. Note 5: the rough line segments under each row indicate the time period consumed by the selected function call in the Profile Panel. Each line segment corresponds to the running of the functions.
Analysis panel
Exclusive time is the time when the function itself performs basic operations (excluding subfunction calls. Implicit time is the time used to call the function (including the time used by subfunction calls. Column 1: "Name" indicates the function Name. Double-click the function name to view the time consumed by the Timeline Panel on the upper half of the page. (Marked with a rough line segment ). Double-click "+" on the left of the function name to expand it. The "parents" and "children" columns of the function are 2: "incl %" indicates the percentage of the aggregate time of the function in the entire MethodTracing time. Column 3: "cumulative sive" indicates the cumulative time. Column 4: "Excl %" indicates the percentage of the function's Exclusive time in the entire MethodTracing time. Column 5: "Exclusive" indicates Exclusive time. Column 6: "CILS + recurcils/Total" indicates the number of Calls to the function (including recursive Calls ). The nativeFinished () column of 2 "14 + 0" indicates 14 non-recursive calls and 0 recursive calls. column 7: new versions (such as 2.1) and "time/CILS" indicate the average call time (that is, the cumulative time/total CILS ). 3. Figure 2 from the google documentation feels old. NOTE: If function A calls function B, function A is called the "parents" of function B, and function B is called the "children ."
For more information about traceview tools, see http://developer.android.com/intl/zh-cn/tools/debugging/debugging-tracing.html.