Support 16 KB page sizes

Historically, Android has only supported 4 KB memory page sizes, which has optimized system memory performance for the average amount of total memory that Android devices have typically had.

However, as device manufacturers continue to build devices with larger amounts of physical memory (RAM), many of these devices will likely be configured with 16 KB (and eventually greater) page sizes to optimize the device's performance. We expect device manufacturers will further optimize performance on future 16 KB devices just as they do with 4 KB devices. Adding support for 16 KB devices enables your app to run on these devices and helps your app benefit from the associated performance improvements.

Benefits and performance gains

Devices configured with 16 KB page sizes use slightly more memory on average, but also gain various performance improvements for both the system and apps:

  • Lower app launch times while the system is under memory pressure: 3% lower on average
  • Reduced power draw during app launch: 4.56% reduction on average
  • Faster camera launch: 4.48% faster hot starts on average, and 6.60% faster cold starts on average
  • Improved system boot time: improved by 1.5% (approximately 0.8 seconds) on average

These improvements are based on our initial testing, and results on actual devices will likely differ. We'll provide additional analysis of potential gains for apps as we continue our testing.

Check if your app is impacted

If your app uses any native code, then you should rebuild your app with support for 16 KB devices. If you are unsure if your app uses native code, you can use the APK Analyzer to identify whether any native code is present.

If your app only uses code written in the Java programming language or in Kotlin, including all libraries or SDKs, then your app already supports 16 KB devices. Nevertheless, we recommend that you test your app in a 16 KB environment to verify that there are no unexpected regressions in app behavior.

Does your app use native code?

Your app makes use of native code if any of the following apply:

  • Your app uses any C/C++ (native) code. If your app uses the Android NDK, then your app uses native code.
  • Your app links with any third-party native libraries.
  • Your app is built by a third-party app builder that uses native libraries on device.

Identify native libraries using APK Analyzer

APK Analyzer is a tool that lets you evaluate various aspects of a built APK. To identify whether your app uses native code or libraries, follow these steps:

  1. Open Android Studio, then click File > Open and choose any project.
  2. From the menu bar, click Build > Analyze APK...

    Studio Build menu option to launch APK
Analyzer

  3. Choose the APK you want to analyze.

  4. Look within the lib folder, which hosts shared object (.so) files if any are present. If any shared object files are present, your app uses native code. If no shared object files are present or there is no lib folder, then your app doesn't use native code.

    APK Analyzer view showing that shared object files are
present

Build your app with support for 16 KB devices

To support 16 KB devices, apps that use native code should complete the steps that are outlined in the following sections.

Compile your app using 16 KB ELF alignment

16 KB devices require ELF segments to be aligned properly using 16 KB ELF alignment in order for your app to run. To compile your app using 16 KB ELF alignment, complete the steps in one of the following sections depending on the version of the Android NDK that you're using.

Android NDK r26 and lower

Specify the following flags when linking:

-Wl,-z,max-page-size=16384

Android NDK r27 and higher

ndk-build

In your Application.mk:

APP_SUPPORT_FLEXIBLE_PAGE_SIZES := true

CMake

In your CMake args, pass -DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON.

Other build systems

Specify the following flags when linking:

-Wl,-z,max-page-size=16384

Check for code instances that reference specific page sizes

Even if your app is 16 KB-aligned, your app can encounter errors if places in your code assume that a device is using a specific page size. To avoid this, complete the following steps:

  1. Remove any hard-coded dependencies that reference the PAGE_SIZE constant or instances in your code logic that assume that a device's page size is 4 KB (4096).

    Use getpagesize() or sysconf(_SC_PAGESIZE) instead.

  2. Look for usages of mmap() and other APIs that require page-aligned arguments and replace with alternatives where necessary.

Test your app in a 16 KB environment

After you build your app with support for 16 KB devices, you'll want to test your app in a 16 KB environment to see whether your app experiences any regressions. To do this, use one of the following testing environments: