Android Runtime Improvements

Android Runtime (ART) is an application runtime environment used by the Android operating system. Replacing Dalvik, the process virtual machine originally used by Android, ART performs the translation of the application’s bytecode into native instructions that are later executed by the device’s runtime environment.(ART introduced in Android L).

Every year google make changes in Android Run time to improve user experience and device performance.Before going to further on improvements let’s first discuss about ART in brief.

What is ART- ART is software layer between applications and operating system.It provide mechanism for executing java language.ART perform two major things to achieve this
1.Runs Android framework and Applications using hybrid model of Interpreter, JIT and profile based Ahead of time compilation(AOT).
2.Memory Management using Memory allocator and Concurrent compacting Garbage collector.

Changes Already happened in the previous years
1.Profile guided ahead of time compilation to improve app startup time,reduce memory usage and reduce storage requirements. Previously in Dalvik whenever app is launched app bytecode is decompiled every time.

2. Android 7.0 Nougat introduced JIT again to remove the need for optimizing apps during system upgrades and reboot. The JIT compiler complements ART’s current Ahead of Time compiler and helps improve runtime performance.

3. Improved Concurrent compacting garbage collector to reduce RAM requirements

What’s New in ART in Android P

  1. Kotlin optimizations-New Compiler optimization to help accelerate the performance in Kotlin code.Changes are made in Kotlinc compiler and D8/R8 bytecode converters to improve kotlin code performance rather than direct changes in ART.Improved Auto vectorization of loops.

2. Memory and storage optimization-This will be more helpful to entry level devices(i.e.Android Go devices with less memory and storage) to perform smoothly.
CompactDex(new dex format)-To reduce the amount of space and memory consumption by app we have to reduce dex files size by shrinking dex codes. Major part of Dex files consist code item instructions and StringData, so by reducing these sections we can optimize dex size. 
When 64k Class methods crossed in android code multiple dex file is created that have duplication of some data(i.e.StringData) so in Android P Runtime “Shared data section ” is introduced inside Vdex Container.
Dex layout optimizations are also done to improve locality in code.Because During application usage only required parts is loaded into memory so improved locality provide startup time benefits and reduction in memory usage.

Dex processing

3. Cloud profiles-In Android P Cloud profiling is introduced to increase advantage of profile guided optimisations. Profiles are first introduced in Android N,it is the metadata Android run time captures about the application execution .This profile data will be used to profile guided optimization process.

How Profiling works-After we install app from playstore ART((JIT and interpreter)) decompile app and perform some optimization on the code. JIT system records this information in profile files..When device comes in idle mode these profiles are used for profile guided optimization and original app code is replaced with this optimized app code.Now this code is already and interpreted ,just in time compiled and pre-optimized. So app startup performance improved over the time user uses that app.

Advantage of Profile Guided Optimization-

  • Faster startup
  • Reduced memory footprint
  • Less jank
  • Less disk space used
  • Increased Battery life

How Cloud profiling works- Profiles data from all the devices are uploaded on play console and a aggregate core profiles will be generated and whenever a new user install app, this core profile will be delivered alongside of app.So for new user profile guided optimisation will be performed right from first time install that will deliver improved cold startup and steady state performance.
Core profile data can also be used by developers to tune application code.
Profile aggregated using cloud profiling will provide a stable core profile by only 20–40 profiles from user. so if we release our app in alpha, beta channels then our productions user will get this core profile from start.

Cloud profiling

So that’s all about the changes in Android run time in Android P. For better understanding you can see full video here.

Also checkout following post in android.