Reader small image

You're reading from  Android Studio 4.1 Development Essentials – Java Edition

Product typeBook
Published inMay 2021
PublisherPackt
ISBN-139781801815161
Edition1st Edition
Right arrow
Author (1)
Neil Smyth
Neil Smyth
author image
Neil Smyth

Neil Smyth has over 25 years of experience in the IT industry, including roles in software development and enterprise-level UNIX and Linux system administration. In addition to a bachelor’s degree in information technology, he also holds A+, Security+, Network+, Project+, and Microsoft Certified Professional certifications and is a CIW Database Design Specialist. Neil is the co-founder and CEO of Payload Media, Inc. (a technical content publishing company), and the author of the Essentials range of programming and system administration books.
Read more about Neil Smyth

Right arrow

82. A Guide to the Android Studio Profiler

Introduced in Android Studio 3.0, the Android Profiler provides a way to monitor CPU, networking and memory metrics of an app in realtime as it is running on a device or emulator. This serves as an invaluable tool for performing tasks such as identifying performance bottlenecks in an app, checking that the app makes appropriate use of memory resources and ensuring that the app does not use excessive networking data bandwidth. This chapter will provide a guided tour of the Android Profiler so that you can begin to use it to monitor the behavior and performance of your own apps.

82.1 Accessing the Android Profiler

The Android Profiler appears in a tool window which may be launched either using the View -> Tool Windows -> Android Profiler menu option or via any of the usual toolbar options available for displaying Android Studio Tool windows. Once displayed, the Profiler Tool window will appear as illustrated in Figure 82-1:

Figure 82-1

In the above figure, no processes have been detected on any connected devices or currently running emulators. To see profiling information, an app will need to be launched. Before doing that, however, it may be necessary to configure the project to enable advanced profiling information to be collected.

82.2 Enabling Advanced Profiling

If the app is built using an SDK older than API 26, it will be necessary to build the app with some additional monitoring code inserted during compilation in order to be able to monitor all of the metrics supported by the Android Profiler. To enable advanced profiling, begin by editing the build configuration settings for the build target using the menu in the Android Studio toolbar shown in Figure 82-2:

Figure 82-2

Within the Run/Debug configuration dialog, select the Profiling tab and enable the Enable advanced profiling option before clicking on the Apply and OK buttons.

82.3 The Android Profiler Tool Window

An active Profiler tool window monitoring a running app is shown in Figure 82-3.

Figure 82-3

The Sessions panel (marked A) lists both the current profiling sessions and any other stored sessions performed since Android Studio was last launched. To the right of the Sessions panel is the live profiling window. The window will continue to scroll with the latest metrics unless it is paused using the Live button (B). Clicking on the button a second time will jump to the current time and resume scrolling. Horizontal scrolling is available for manually moving back and forth within the recorded timeline.

The top row of the window (C) is the event time-line and displays changes to the status of the app’s activities together with other events such as the user touching the screen, typing text or changing the device orientation. The bottom time-line (D) charts the elapsed time since the app was launched.

The remaining timelines show...

82.4 The Sessions Panel

When an app is running and the Profiler tool window displayed, the profiler will automatically attached to the app and begin profiling. An entry showing the app name, the device or emulator on which it is running and the start time will appear in the Sessions panel as shown in Figure 82-5. The green circle next to the time indicates a currently active profiling session. Pressing the red stop button will end the current session but the data and graphs will remain available for browsing until Android Studio exits. Additional profiling sessions can be started by clicking on the + button and selecting the device and app:

Figure 82-5

The green circle next to the time indicates a currently active profiling session. Pressing the red stop button will end the current session but the data and graphs will remain available for browsing until Android Studio exits. Additional profiling sessions can be started by clicking on the + button and selecting the device...

82.5 The CPU Profiler

When displayed, the CPU Profiler window will appear as shown in Figure 82-8. As with the main window, the data is displayed in realtime including the event time-line (A) and a scrolling graph showing CPU usage (B) in realtime for both the current app and a combined total for all other processes on the device:

Figure 82-8

Located beneath the graph is a list of all of the threads associated with the current app (C). Referred to as the thread activity timeline, this also takes the form of a scrolling time-line displaying the status of each thread as represented by colored blocks (green for active, yellow for active but waiting for a disk or network I/O operation to complete or gray if the thread is currently sleeping).

The CPU Profiler supports two types of method tracing (in other words profiling individual methods within the running app). The current tracing type, either sampled or instrumented, is selected using the menu marked D. The tracing types...

82.6 Memory Profiler

The memory profiler is displayed when the memory time-line is clicked within the main Android Profiler Tool window and appears as shown in Figure 82-14:

Figure 82-14

The memory time-line shows memory allocations relative to the scale on the right-hand side of the time-line for a range of different categories as indicated by the color key. The dashed line (A) represents the number of objects allocated for the app relative to the scale on the left-hand side of the time-line graph.

The trash can icons (B) indicate garbage collection events. A garbage collection event occurs when the Android runtime decides that an object residing in memory is no longer needed and automatically removes it to free memory.

In addition to the usual timelines, the window includes buttons (C) to force garbage collection events and to capture a heap dump.

A heap dump (Figure 82-15) lists all of the objects within the app that were using memory at the time the dump was...

82.7 Network Profiler

The Network Profiler is the least complex of the tools provided by the Android Profiler. When selected the Network tool window appears as shown in Figure 82-17:

Figure 82-17

In common with the other profiler windows, the Network Profiler window includes an event timeline. The Radio time-line (marked A in Figure 82-17) shows the power status of the radio relative to the Wi-Fi connection if one is available.

The time-line graph (B) includes sent and received data and a count of the number of current connections. At time of writing, the Network Profiler is only able to monitor network activity performed as a result of HttpURLConnection and OkHttp based connections.

To view information about the files sent or received, click and drag on the time-line to select a period of time. On completing the selection, the panel labeled A in Figure 82-18 will appear listing the files. Selecting a file from the list will display the detail panel (B) from which...

82.8 Energy Profiler

The energy profiler (Figure 82-19) provides a realtime analysis of the energy used by the currently running app categorized in terms of CPU, networking and location tracking activity.

Figure 82-19

The Energy profiler includes an event timeline (A) and a graph indicating the current energy usage (B). The graph is colored based on the key (C) which also indicates the current energy usage levels of the three categories. Selecting a section of the graph will list any system processes that are contributing to excessive energy use such as alarms and jobs, location requests or wake locks.

82.9 Summary

The Android Profiler monitors the CPU, memory, network and energy resource usage of apps in realtime providing a visual environment in which to locate memory leaks, performance problems and the excessive or inefficient battery use or transmission of data over network connections. Consisting of different profiler views, the Android Profile allows detailed metrics to be monitored, recorded and analyzed.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Android Studio 4.1 Development Essentials – Java Edition
Published in: May 2021Publisher: PacktISBN-13: 9781801815161
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime

Author (1)

author image
Neil Smyth

Neil Smyth has over 25 years of experience in the IT industry, including roles in software development and enterprise-level UNIX and Linux system administration. In addition to a bachelor’s degree in information technology, he also holds A+, Security+, Network+, Project+, and Microsoft Certified Professional certifications and is a CIW Database Design Specialist. Neil is the co-founder and CEO of Payload Media, Inc. (a technical content publishing company), and the author of the Essentials range of programming and system administration books.
Read more about Neil Smyth