You can start a method trace from the CPU Monitor included with Android Monitor. It lets you view the call stack and timing information for your app. This information can help you optimize and debug your app.
Understanding the Method Trace Display
The method trace looks similar to the following figure:
The display shows the following information:
| Field | Description |
|---|---|
| Name | The name of the method. |
| Invocation Count | How many times the method was called. |
| Inclusive Time (microseconds) | Time spent in the method and all of its children, either wall clock or thread time, depending on your selection in the x-axis menu. |
| Exclusive Time (microseconds) | Time spent just in the method (excluding time spent in its children), either wall clock or thread time, depending on your selection in the x-axis menu. |
Note: Running the method trace significantly affects CPU timings. Use the method trace to understand the flow of the program, but not for performance timings.
Performing a Method Trace in the CPU Monitor
To perform a method trace:
- Display a running app in the CPU Monitor.
- Start a trace by clicking Start Method Tracing
to
select it.
- To stop the trace, click Stop Method Tracing
to
deselect it.
- Specify display options:
- Select a Thread.
- Select an x-axis time for the graphic and the method list:
- Wall Clock Time - Total CPU time elapsed between the method call and return.
- Thread Time - Total time during which the JRE scheduled the thread during call processing. It’s less than or equal to the Wall Clock Time: less if the JRE interrupted the thread, and equal if it didn’t. The thread might not run continuously; when it’s not executing, that time is excluded. If threads are interrupted often and it’s not by design, the interruptions affect app performance. However, an example of a by-design use is synchronous operations that take a long time, such as file transfers and reads from disk, where the method could be the asynchronous wrapper for the synchronous reader.
- Optionally select Color by inclusive time.
The graphic represents the wall clock or thread time for each method. Hover the cursor over the display to receive information about the method. This information also appears in the table.
The method trace appears in the Code Editor area.
Android Studio creates the method trace file
with the filename package_yyyy.mm.dd_hh.mm.ss.trace
using the activity package (or project) name, year, month, day, hour, minute, and second of
the capture, for example,
com.android.calc_2015.11.17_14.58.48.trace.
Viewing a Saved Method Trace
After you do a method trace, Android Studio automatically stores it so you can view it again.
To examine a saved method trace:
- Click
in the main window.
- Open the Methods Tracing folder.
- Double-click the file to view it.
Or select View > Tools Windows > Captures.
The Captures window appears.
Sorting Method Trace Data
You can sort the data by method name, count, inclusive time, and exclusive time.
To sort method trace data:
- Click a column heading to sort the table by ascending or descending order.
Working with Method Trace Files
You can rename, locate, and delete a method trace file from within Android Studio.
Renaming a method trace file
Rename a method trace file from within Android Studio so it continues to appear in the Captures window.
To rename a method trace file:
- In the Captures window, right-click the file and select Rename.
- In the dialog, specify the name of the file and click OK.
Locating a method trace file on disk
You can quickly discover where Android Studio stored method trace files on disk.
To locate a method trace file on disk:
- In the Captures window, right-click a method trace file and select Show or Reveal.
Android Studio opens an operating system file browser displaying the location where the file resides.
Note: If you move a method trace file, Android Studio no longer displays the file in the Captures window. To display it, use File > Open. Also, rename a file from the Captures window and not in the operating system file browser.
Deleting a method trace file
To delete a method trace file:
- In the Captures window, right-click a method trace file and select Delete.
Android Studio deletes the file from the Captures dialog and from disk.