visit
The Android Profiler is first of all good because it is integrated into Android Studio and is designed specifically for profiling Android applications. However, it has a number of disadvantages, which you can find on StackOverFlow or Issue Tracker: hangs, crashes, incorrect data (over- or underestimated time), not always attached to the process.
This is especially noticeable on large commercial projects (small projects usually have no problems). In such cases, it is recommended to use Perfetto.
It is worth noting that Perfetto also has disadvantages, but works more stably. Its disadvantages are mainly related to the inconvenience of work. Perfetto knows how to work not only with Android applications. It integrates with
Related to this is another advantage of Perfetto - it sees the whole picture, not just the Android app. This means that if another process has affected the application process, you can see it through Perfetto. This is not available to Profiler.
Another downside toPerfetto is that it is only available with Android 9 (P, api 28). And it needs to be activated. With Android 11 (R, api 30) it is enabled by default. To activate it, you need to enter:
adb shell setprop persist.traced.enable 1
There are several ways of working with Perfetto. Let's look at one of the most obvious ones.
Let's take PerfettoActivity as an example. Create a project and insert the following code:
class PerfettoActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Trace.beginSection("Perf: printString")
Thread.sleep(2000)
Trace.endSection()
}
}
The point of this example is that we delay the main thread by 2000ms and wrap it in a Trace.beginSection() and Trace.endSection() section. With these methods it will be easier for us to find in the renderer.
Next, go to Device Settings -> System -> Advanced -> Developer options -> System Tracing
It is necessary to enable the Show Quick Settings tile option. When this option is checked, the Record trace button in the quick settings becomes available.
Next, go to Quick settings, clickRecord trace, launch the application and click Stop tracing. A file at data/misc will appear in the device memory. In order to retrieve it, you must enter the command: adb pull /data/local/traces
After that, the traces folder will appear in the root of the project, where the necessary file will be located:
Then go to:
There is a visualizer available atui.perfetto.dev for easy viewing and analysis of the results. The new visualizer takes advantage of modern Web platform technologies. Its multi-threaded design, based on WebWorkers, provides a consistently responsive user interface. You can read more about it
The Perfettouser interface, once opened once, works completely offline. Traces opened with the UI are handled locally by the browser and require no server-side interaction.
In the panel on the left, click the Open trace file button. Find the .perfetto-trace file just generated by Android Studio and click Open.
The following result appears:
The picture shows all the processes and threads that take place on the device. The colored bars show the methods running in these threads.
Let's find the process of our application in the list. In my case it is com.leonidivankin.draftandroid (see point 1).
Here we find the main thread (point 2). As you see, its name is not main. In this line you can see the methods executed when creating and starting the activation (point 3).
They also have different names, in particular you can find theperformCreate:com.leonidivankin.draftandroid.articles.perfetto.PerfettoActivity - this is the onCreate() method in Android concepts. As I see it, this is the main drawback of Perfetto before Profiler - the inconsistency of thread and method names relative to the source code.
Here we see our section marked with the Perf: printString tag (point 4). Click on this section and a window opens with additional information Current Selection:
Here you can see additional information about the work done in the section (point 2), in particular the time of completion (point 3).
You can also use the search bar (point 1) and enter there the Perf: printString tag you are looking for. The Perfettovisualizer highlights the desired process (point 2), thread (point 3), and segment (point 4).
Using control+scroll, you can zoom in and out:
Scroll down and up through the Perfetto panel. You may notice processes you are familiar with, such as bluetooth, audio.service, composer, systemui, and others:
This way you can establish dependencies between your application and others and look for problems in their dependencies. From my point of view, this is the most important advantage of Perfetto over Profiler.
Advantages:
Disadvantages:
Perfetto is a quite powerful tool when you need to look inside the processes running on your device. It is most often used to analyze applications and optimize, for example, the cold-warm-hot start speed of an application. It has several advantages. For example, analyzing all the processes as a whole and getting more accurate results. However, as you have seen, it is not without disadvantages.