To generate a method trace of your app's execution, you can instrument your app
class. Instrumenting your app this way gives you more control over exactly when
the device starts and stops recording tracing information. The device also saves
your trace logs using the names you specify, so you can easily identify each log
later. You can then view each trace log using Android Studio or
Another way to trace your app, that doesn't require you to instrument your app's code, is to start and stop method tracing using Android Studio's CPU profiler. To learn more, read Inspect CPU Activity and Method Traces with CPU Profiler.
Before you start generating trace logs, make sure your app has
permission to write to external storage
so that it can save trace logs to the device.
Instrument your app
To create trace logs, call
where you want the system to start logging tracing data.
In the call, you can specify the name for the
.trace file, and the system saves it to a package-specific
directory that's intended for persistent app data on the target device—this is
the same directory that is returned by
and is located in the
~/sdcard/ directory on most devices.
This file contains the binary method trace data and a mapping table with thread
and method names. To stop tracing, call
The following sample starts and stops recording a trace log with the name
// Starts recording a trace log with the name you provide. For example, the // following code tells the system to start recording a .trace file to the // device with the name "sample.trace". Debug.startMethodTracing("sample"); ... // The system begins buffering the generated trace data, until your // application calls
stopMethodTracing(), at which time it writes // the buffered data to the output file. Debug.stopMethodTracing();
Note that if your app calls the
again without changing the name of the trace log, it overwrites the existing log
saved to the device. To learn how to dynamically change the name of each trace
log, go to the section about saving multiple logs.
If the system reaches the maximum buffer size before you call
the system stops tracing and sends a notification to the console.
The methods that start and stop traces work across your entire app process. That
is, you could call
in your activity's
method, and call
in that activity's
Note that your app runs more slowly when profiling is enabled. That is, you
shouldn't use the profiling data to determine absolute timings (such as, "method
foo() takes 2.5 seconds to run"). The timing information in the trace logs are
useful only when comparing it to previous trace logs, so you can see if recent
changes make your app faster or slower.
When deploying to devices running Android 5.0 (API level 21) and higher, you can
use sample-based profiling to profile with less runtime performance impact. To
enable sample profiling, call
(instead of calling
startMethodTracing()) with a specified sampling
interval. The system gathers samples periodically until your app calls
Save multiple logs
If your app starts and stops a method trace multiple times without specifying a
new name for the trace log, the device overwrites the older trace log with the
new one—that is, it only keeps the most recent trace log. To save multiple
trace logs to your device, dynamically rename the trace log each time your app
The sample below uses the
class to include the current date and time when naming each trace log:
// Uses the
SimpleDateFormatclass to create a String with // the current date and time. SimpleDateFormat date = new SimpleDateFormat("dd_MM_yyyy_hh_mm_ss"); String logDate = date.format(new Date()); // Applies the date and time to the name of the trace log. Debug.startMethodTracing( "sample-" + logDate);
Access trace logs on the device
After the system creates the trace log on your device, you can access the file in one of the following ways:
Use the Device File Explorer. To open the Device File Explorer, click View > Tool Windows > Device File Explorer (or click the Device File Explorer button in the tool window bar). As shown in figure 1 you can locate the
.tracefiles by navigating to your app's package-specific directory.
To quickly view the trace log within the IDE's editor window, as shown in figure 2, double-click the file. You can also right-click the file to save it to your local disk and then use Traceview to inspect more detailed information, such as parent and child methods, average execution times per method call, and CPU versus real time. If you don't see the log files, try right-clicking on the directory you expect to see them and selecting Synchronize.
Copy the file to your local machine using the
adb pullcommand. The command below copies a trace log named
sample.tracefrom the device to the
~/Documents/trace-logs/directory of your local machine. You can then view the
.tracefile using Traceview.
adb pull path-on-device/sample.trace ~/Documents/trace-logs/