Chapters

Hide chapters

Android Debugging by Tutorials

First Edition · Android 12 · Kotlin 1.6 · Android Studio Chipmunk (2021.2.1)

Section I: Debugging Basics

Section 1: 8 chapters
Show chapters Hide chapters

12. Android Energy Profiler
Written by Zahidur Rahman Faisal

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In the modern day, we can’t live a single moment without our smartphones. Most of the time, we’re either messaging, listening to music, watching videos or snapping pictures with our phones! To serve our needs all day long, smartphones are getting more and more powerful in terms of CPU, battery or storage. Even after having a smartphone with huge battery life, we all see the ‘Low Battery’ alert or run out of battery frequently. This indicates that no matter how powerful your phone is, the apps you use also need to smartly manage energy consumption so you can keep using them as long as you need.

In the previous chapter, you learned to profile network activity and optimize network resource consumption.

In this chapter, you’ll learn to inspect energy consumption with the Energy Profiler that comes with Android Studio.

In the process, you’ll learn about:

  • How to use different components of the Energy Profiler tool.
  • How to identify resource-hungry events, such as wake locks, jobs and alarms.
  • Optimizing energy consumption and monitoring changes.

Overview of The Energy Profiler

The Energy Profiler is a part of Android Profiler tools in Android Studio 3.0 and onwards. The Android Profilers help you investigate different app performance aspects, such as CPU usage, network or radioactivity, GPS & sensor data. The Energy Profiler displays a visualization of how much energy each of these components uses. It also shows you occurrences of system events such as wake locks, alarms, jobs, and location requests, that impact energy consumption.

Energy Profiler appears as a row in the Profiler window once you run your app on a connected device or Android Emulator running Android 8.0 (API 26) or higher.

Follow these steps to open the Energy Profiler:

  1. Open the Podplay starter project and run the app on an emulator or connected device using API level 26 or higher.

  2. Select View ▸ Tool Windows ▸ Profiler from the menu bar.

  3. This will start a new profiling session for PodPlay within the Android Profiler. Click over the ENERGY timeline from the right pane as displayed below:

This will open the actual Energy Profiler, and it’s a detailed representation of the energy consumption from your app in real-time. The new panel will look like this:

As shown above, the Energy Profiler panel includes the following main components:

  1. Sessions Pane: Displays your current session info, such as starting time, elapsed time in seconds, and the device name. The sessions pane allows you to start a new profiling session or stop the current session. If you re-launch the app, a new session will be added to this pane with the current timestamp. The session data remains here until you quit Android Studio.

  2. Event Timeline: This timeline shows user interactions with the device, such as keyboard inputs, pressing hardware buttons or screen rotation events.

  3. Energy Timeline: This shows the estimated energy consumption from your application code. This includes CPU loads, Network activity etc.

  4. System Timeline: System events that may affect energy consumption are displayed in this timeline. They could be alarms, scheduled jobs or wake locks. You’ll get to more details about system events shortly.

  5. Quick Preview: Using your cursor, hover over any point on the Energy Timeline. It’ll display a quick preview of energy consumption from your app at that point.

  6. Timeline Controls: These buttons offer you control over the timelines. The utility of them are as follows:

  • Zoom Out: Zooms out of the timeline, and displays a longer period to allocate more requests, if there are any, in the timeline view.

  • Zoom In: Zooms in on the timeline displaying a shorter period in the timeline view.

  • Reset Zoom: Resets any zoom-out or zoom-in over the timeline view and reverts to the default zoom state.

  • Zoom to Selection: This allows you to zoom into any selected interval from the timeline. This is helpful to focus on a specific time frame that you select to identify a network request which occurred within that period.

  • Attach to Live: This button allows you to pause the timeline at any point or resume displaying the real-time updates jumping to the end of the timeline.

Note: Selection over the timelines in the Energy Profiler is not allowed, so Zoom to Selection will remain disabled.

You might be curious to know more regarding energy management, for example, terms like system events, before putting those components in use!

Android Energy Management 101

Android runtime has a few techniques to ensure your device stays alive and performant for an extensive period. There are system events that consume energy and defense mechanisms that protect the device from unnecessary energy drain. You need to clearly understand these to make your app energy-efficient.

System Events: Wake Locks, Jobs, and Alarms

Energy Profiler logs events that are triggered by the system that can affect energy consumption. These events can be a scheduled background task, a data request using sensors and more.

Doze Mode

From Android 6.0 (API level 23) onwards, Android introduced two power-saving features that extend battery life by managing how apps behave in different conditions. One of them is putting the device on Doze mode when it’s not connected to a power source, in simple words, charging.

wmdeoc elw xleluufemd um venjagr Kuqo Tuti daebsujuqde yosbid resu

App Standby

App Standby is another energy-saving technique that’s used to prevent unnecessary system events. It’s a state where the system determines if an app is idle or not in use. When in the standby state, apps are deferred from background or network activity.

Inspecting and Optimizing Energy Consumption

You must be eager to get your hands on the Energy Profiler and see how you can minimize energy consumption from PodPlay!

override fun onDestroyView() {
  super.onDestroyView()
  releaseWakeLock()
  _databinding = null
}

Best Practices for Energy Management

As a developer, you’re in the ultimate control to code your app in a way that allows it to operate with minimal energy and resource consumption. Below are some best practices you can follow while developing your next app:

Key Points

  • Use the Energy Profiler to monitor, inspect and detect energy-related issues.
  • Check out detailed energy stats at any point with the Energy and System events timeline.
  • System events are resource-hungry! Be mindful while using them.
  • Avoid triggering unnecessary network calls, background tasks or location requests to keep your app energy efficient.

Where to Go From Here?

That ends your adventure of diving as a debugger into the ocean of code and finding the finest solutions!

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2024 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now