macOS Energy Efficiency Guide

From Lazarus wiki
Revision as of 05:24, 27 February 2021 by Trev (talk | contribs) (Activity Monitor, Instruments, fs_usage: Add fs_usage utility)

This article applies to macOS only.

See also: Multiplatform Programming Guide

English (en)


Warning: Work in progress


All applications consume energy whenever they update the user interface, perform networking operations, write to disk, or run code on the CPU. As users rely increasingly on battery power energy efficiency becomes integral to the user experience.

macOS employs advanced energy-saving technologies to help users get the most out of their Macs. These features help the operating system make smart decisions about how to use resources and run code as efficiently as possible.

Quality of Service Levels

Tasks that affect the user, such as downloading and playing music, take priority over background and discretionary work. Quality of service class APIs allow you to assign distinct priority levels to the work your application performs, giving you fine grained control over task prioritization.

Event-Based APIs and Services

Timers deliver events, or fire, at prescribed time intervals. If a timer fires when the system is idle, the CPU and numerous other systems awake from their low-power states. Yet many of these systems aren’t always needed to perform the work invoked by the timer. If the work can be performed when the system hardware is already running, the additional cost is not incurred and the CPU can remain idle longer. macOS provides services and APIs that efficiently deliver events without unnecessarily waking the CPU.

App Nap

When your application is not busy performing user-initiated work, the system may put it in App Nap. App Nap conserves energy by regulating your application’s CPU usage, I/O, and timers. As soon as the user resumes interacting with your application, macOS switches it back to full speed. Transitions are so seamless that the user thinks your application has been running at full speed all along. You can enhance App Nap by implementing notifications that tell your application when it becomes inactive, so it can immediately start reducing activity.

For further details about App Nap, including FPC code, see macOS App Nap.

Activity Monitor, Instruments, fs_usage

The Activity Monitor application lets you quickly identify applications that are using significant amounts of energy in macOS. The Xcode Instruments application, and numerous command-line tools, help you identify and address energy problems as you develop your application, rather than after those problems are encountered by users.

The command line fs_usage utility provides a running list of system calls and page faults related to activity in the file system. It has options that allow you to specify a timeout, filtering, and more. For details of its operation and options, open an Applications > Terminal and man fs_usage.

Minimise I/O

I/O-related tasks, such as writing to disk, bring the system out of an idle state. Writing to flash memory is especially energy-draining. By writing data less, aggregating writes together, using caching wisely, scheduling network transactions, and minimizing overall I/O, you can boost the energy efficiency and performance of your application.

External links