The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.
CPU issues tend to manifest themselves in three ways:
Regardless of how the issue appears to the user, in the end, it is a
matter of you using too much CPU time. That could be simply because
your application is written to be constantly active (e.g., you have
an everlasting service that uses
TimerTask to wake up every second
and do something). There is little anyone can do to help that short
of totally rethinking the app’s architecture (e.g., switch to
AlarmManager and allow the user to configure the polling period).
However, in many cases, the problem is that you are using algorithms – yours or ones built into Android — that simply take too long when used improperly. This chapter will help you identify these bottlenecks, so you know what portions of your code need to be optimized in general or apply the techniques described in later chapters of this part of the book.
Understanding this chapter requires that you have read the core chapters and understand how Android apps are set up and operate. Reading the introductory chapter to this trail is also a good idea.
In Android Studio, the Android Profiler tool allow you to examine the real-time behavior of your app with respect to various system resources, such as the CPU.
If one or more debuggable processes are running on an attached device or an emulator, the Android Profiler tool will appear as an option on the bottom dock:
Figure 970: Android Studio Bottom Tool Dock, Showing Android Profiler
Opening the profiler will give you a real-time look at the consumption of CPU, heap space, and network associated with your process:
Figure 971: Android Profiler
The top area will show a green bar when your UI is in the foreground, with a note as to what activity of yours is in the foreground. Purple dots show when you have received user input, such as taps on buttons.
Across the top, there are:
Hovering your mouse over a portion of the graph will show you the specific value at that point in time for the particular metric you are hovering over:
Figure 972: Android Profiler, Showing Specific CPU Usage
Clicking on the CPU graph at any point shows you more details about the per -thread CPU utilization:
Figure 973: Android Profiler, Showing Per-Thread CPU Usage
Android Studio 3.0’s Profiler has issues. Depending on the nature of your app, you may find that the profiler dramatically slows down your app.
Use the red X button to close the profiler, and your app should return to normal.
The preview of this section is sleeping in.
The preview of this section was eaten by a grue.
The preview of this section is in the process of being translated from its native Klingon.