Android N introduces a hybrid runtime using compilation + interpretation + JIT to obtain the best compromise between installation time, memory footprint, battery consumption and performance.
One of the main features introduced as preview in Android KitKat and as the default solution in Lollipop was ART, a new runtime at that time. ART replaced Dalvik but it was bytecode compatible with it because it was and still is executing DEX files. One of ART’s main characteristics was the AOT compilation of applications during installation. The main benefit was optimized native code that performed better and required less power to be executed. The downside was space and installation time. Large applications need minutes to be installed under Lollipop and Marshmallow.
Android N Developer Preview includes a hybrid runtime. There won’t be any compilation during install, and applications can be started right away, the bytecode being interpreted. There is a new, faster interpreter in ART and it is accompanied by a new JIT, but the JIT information is not persisted. Instead, the code is profiled during execution and the resulted data is saved. Later, when the device is idle and charging, ART will perform a profile-based compilation of the hot sections of the code, the rest remaining untouched. To produce optimized code, ART uses several techniques including deep in-lining.
Depending on what the profiler finds during subsequent executions, the compilation process can be applied multiple times on the same application, either finding new code paths that have become hot or by applying new optimizations to the already compiled code. While this procedure is still called AOT for simplicity, it can be read All-Of-the-Time compilation.
The overall benefits of this mixed AOT + Interpreted + JIT strategy are:
- the installation time even for large applications is reduced to seconds
- system updates are installed faster because they do not require an optimization step
- the RAM footprint of the application is smaller, in some cases down to 50%
- improved performance
- lower battery consumption
To find out even more details on the optimizations applied during compilation, we recommend the podcast Episode 45: State of the ART, having as guests Brian Carlstrom, Leader of the Android ART team, and Anwar Ghuloum, Director of Engineering for the Android Platform.