The evolution of Dalvik and ART compilation methods and the impact of Android N hybrid compilation on hot fixes

Introduction

ART和Dalvik

Android Runtime (ART) is a managed runtime used by applications and some system services on Android. ART and its predecessor, Dalvik, were originally built for the Android project. As a runtime ART can execute Dalvik executable files and follow the Dex bytecode specification.
ART and Dalvik are compatible runtimes that run Dex bytecode, so applications developed for Dalvik can also operate in the ART environment. However, some of the technologies used by Dalvik are not suitable for ART.

JTI 和 AOT

In Android, Java classes are converted into DEX bytecode. DEX bytecode is converted into machine code through ART or Dalvik.
The compilation method of converting DEX bytecode into machine code is different between ART and Dalvik: Dalvik uses JIT (Just in time) compilation and ART uses AOT (Ahead of time) compilation.

Just In Time (JIT)
uses the Dalvik JIT compiler. Every time the application is running, it translates part of the Dalvik bytecode into machine code in real time. During the execution of the program, more code is compiled and cached. Since JIT only translates part of the code, it consumes less memory and takes up less physical storage space.
Ahead Of Time (AOT)
ART has a built-in Ahead-of-Time compiler. During the installation of the application, he translated the DEX bytecode into machine code and stored it in the device's memory. This process only happens when the app is installed on the device. Since JIT compilation is no longer required, the code execution speed is much faster.

The ART virtual machine has become a hybrid AOT+JIT mode after the compilation mode is 7.0.

Why need to understand

Because technologies such as hot repair in Android will be affected by the compilation method, the old hot repair method will fail. The specific impact can be seen in this article on the impact of Tinker hot fix , Android N hybrid compilation and in-depth analysis of the impact of the hot patch , the compatibility strategy of the Android hot fix program CLASS_ISPREVERIFIED problem , other charges or less features will not be analyzed first Up.

Evolution and difference

Android 4.x(Interpreter + JIT)

Principle: Usually the code runs through the interpreter, but the hot trace will execute JIT for instant compilation.
Advantages: less memory
consumption. Disadvantages: power consumption (after exiting the App, the compilation will be repeated next time), freezes (when compiling with JIT)

Android 5.0/5.1/6.0(interpreter + AOT)

Principle: In AOT mode, all compilations will be completed when the App is installed.
Advantages: Good performance
Disadvantages: App installation takes a long time and takes up a lot of storage space.

Android 7.0/7.1 ART introduces a new Hybrid mode (Interpreter + JIT + AOT)

Principle: App is not compiled during installation, so the installation speed is fast.
When running the App, first go to the interpreter, and then the hot functions will be recognized, compiled by JIT, stored in the jit code cache, and a profile file (recording hot function information) will be generated.
When the mobile phone enters the charging and idle state, the system will scan the profile file in the App directory at regular intervals and execute AOT compilation (Google officially calls it profile-guided compilation).
Whether it is the binary code compiled by jit or the binary code compiled by AOT, the performance difference between them is not big because they use the same optimizing compiler for compilation.
Advantages: App installation speed is fast, and it takes up less storage (only compile hot functions).
Disadvantages: The first few runs will be slower, only the more user operations, the performance will keep up after jit and AOT are compiled.

Other understanding of Android 7.0/7.1/N

Android N introduces a hybrid runtime that includes compilation, interpretation, and JIT (Just In Time) to achieve the best compromise between installation time, memory usage, battery consumption, and performance.

ART is one of the main features introduced in Android KitKat (Translator's Note: Android 4.0) and set as the default solution in Lollipop (Translator's Note: Android 5.0). It was a new runtime at that time. ART replaced Dalvik, but the former still maintains bytecode-level compatibility with the latter because the former is still running DEX files. One of the main features of ART is the AOT compilation of the application during installation. The main advantage of this approach is that the native code generated by optimization has better performance and requires less power to execute. The disadvantage is the space and time required to install the files. In Lollipop and Marshmallow (Translator's Note: Android 6.0), large apps take several minutes to install.

The Android N developer preview includes a mixed-mode runtime. The application does not compile during installation, but interprets bytecode, so it can be started quickly. There is a new and faster interpreter in ART, which is completed by a new JIT, but the information of this JIT is not persistent. Instead, the code is analyzed during execution and the analysis results are saved. Then, when the device is idling and charging, ART will perform analysis-based compilation for the "hot code", and other codes will not be compiled. In order to get better code, ART uses several techniques including deep inlining.

You can compile the same application several times, or find a "hot" code path or perform new optimizations on the compiled code, depending on the analysis data of the analyzer in the subsequent execution. This step is still referred to as AOT for short, which can be understood as "All-Of-the-Time compilation".

The full advantages of this mixed use of AOT, interpretation, and JIT strategy are as follows.

  • Even for large applications, the installation time can be reduced to a few seconds
  • System upgrades can be installed faster because there is no need to optimize this step
  • The memory footprint of the application is smaller, in some cases it can be reduced by 50%
  • Improved performance
  • Lower battery consumption

In summary, the ART on Android 7.0/7.1 combines the JIT of Android 4.x with the AOT of Android 5.x/6.0, and learns from each other's strengths to obtain a trade off between performance and battery.

The difference above is quoted from- Zhihu's answer

Guess you like

Origin blog.csdn.net/u011148116/article/details/106791704