Overview
This lesson explores Profile-Guided Optimization (PGO), a cornerstone of modern Android performance. PGO allows the Android Runtime (ART) to make highly targeted compilation decisions based on actual app usage patterns, bridging the gap between JIT and AOT compilation.
Profile Collection During App Use
When an application runs, ART's JIT compiler is constantly monitoring execution. As it identifies hot methods and resolves virtual method calls, it serializes this data into profile files.
These profiles are typically stored in /data/misc/profiles/.
.prof(Primary Profile): Contains data about methods executed and classes loaded during the app's lifetime..profm(Profile Metadata): Contains metadata used to map the profile data back to specific DEX files and versions.
# View the profile for a specific package
adb shell ls -la /data/misc/profiles/cur/0/com.android.systemui/
By profiling the app, ART learns precisely which code paths are critical for startup and smooth UI interactions, and which code paths (like error handling or rarely used features) can remain uncompiled to save storage space.
The speed-profile Compilation Filter
The magic of PGO happens when the device is idle and charging. The background_dexopt service invokes the dex2oat compiler using the speed-profile compilation filter.
When dex2oat runs with --compiler-filter=speed-profile, it reads the .prof file. Instead of compiling the entire application (which is slow and wastes storage), it exclusively AOT-compiles the methods listed in the profile.
This results in a highly optimized OAT file tailored specifically to how the user interacts with the app. The app starts faster and runs smoother, while its storage footprint remains significantly smaller than a fully AOT-compiled app.
Cloud Profiles from Play Store
While local PGO is highly effective, it has a "cold start" problem: the app must be used for a while before a useful profile is generated. Until then, the user experiences the overhead of JIT compilation.
Google introduced Cloud Profiles (Art Cloud Profiles) to solve this. When users around the world run an app, Play Services aggregates their local profiles anonymously.
When a new user downloads that app from the Play Store, Google Play delivers the aggregated "Cloud Profile" alongside the APK. During the installation process, ART uses this Cloud Profile to immediately AOT-compile the app's hot methods using dex2oat.
Because of Cloud Profiles, the app is highly optimized on the very first launch, eliminating the JIT warm-up period entirely.
dexpreopt at Build Time
PGO isn't just for user apps; it is extensively used for the Android framework itself.
During the Android OS build process (AOSP), a tool called dexpreopt is used to pre-optimize framework jars (like framework.jar, services.jar) and system apps.
AOSP includes baseline profiles for core system components. The build system uses dex2oat on the host machine to compile these components against the baseline profiles before the system image is even flashed to a device.
# AOSP Android.bp example enabling dexpreopt with a profile
dex_preopt {
profile: "art-profile",
app_image: true,
}
This pre-optimization is crucial. It ensures that the system boot classpath and the Zygote process are fully optimized, guaranteeing fast boot times and a responsive system UI immediately after flashing or updating the OS.