ART past life and present life

While Android applications are mostly written in Java/Kotlin, the Android VIRTUAL machine does not actually use JVM bytecode, but instead compiles Class files into more compact dex files via the DX compiler (now D8), which are then executed by the virtual machine.

The original Android VIRTUAL machine was Dalvik, and ART virtual machine was introduced as an option by Android4.4. After Android5.0, Dalvik was replaced, and a series of changes were made in Android7.0 and 8.0 respectively. The official documentation about ART virtual machine has been mentioned. But it’s not that easy to understand.

Basic concepts and nouns

  • Dex file: after all Java source code of App is compiled, many class files are generated. DX/D8 is compiled into one or multiple (multiDex) dex files, which are compiled and executed by the Android VM

  • Odex file: the product of the dex file after verification and optimization. The odex file under art contains the aot-compiled code and the complete content of dex, but the dex content in odex has been moved to the. Vdex file after Android8.0

  • Art file: The. Art file is generated at the same time when the odex file is generated according to the configuration file in art, which is mainly to improve the speed of loading the hot codes in Odex during runtime. The art file contains the class information and the index of the hot methods in Odex

  • Interpreter: Used to interpret code line by line while a program is running and translate it into machine code execution for the corresponding platform

  • Compile the JIT (Just In Time) : as the interpreter run too slow to introduce, for * * frequently run hot code (decision criteria are generally In a certain Time period to reach a certain threshold) real-time compile () method for particle size under the ART, and the cache JIT compiled code In memory for execution next Time. Because it is compiled at ArtMethod granularity, JIT editing can produce more efficient code and run faster than an interpreter.

  • AOT compilation (Ahead-of-time) : Fully compiles all code to local machine code during application installation and executes machine code directly at runtime.

How ART works

4.4 ~ 7.0

At the beginning, ART only uses AOT compilation. When the App is installed, all the code is compiled and stored locally, and the App is directly run when opened. The advantage of this method is that the application runs faster, but the disadvantage is also obvious.

7.0

After Android N, ART was changed and JIT compilation was reintroduced. Combined with AOT/JIT compilation, the main mechanism is as follows:

  1. The first few runs run only through interpreter interpretation, while hot code is JIT compiled and its information is recorded in a configuration file

  2. When the device is idle or charged, the compilation daemon reads the configuration file, AOT compiles the hotspot code, and writes it to the odex file corresponding to the APP

  3. Aot-compiled code is preferred when the application is started again, otherwise the process is repeated using interpreter +JIT compilation

For some huge apps, such as Taobao, some functions may not be used in a lifetime. According to the above strategy, this part of the code will not be compiled and saved, thus reducing the storage space. In addition, the time and space cost of fully compiling all existing applications is avoided during system upgrade.

8.0

Android 8.0 introduced the vdex file, which contains the uncompressed DEX code for APK, as well as some metadata to speed up validation. How do you understand that? Here we need to supplement the compilation configuration of dex file and the default compilation mode of various applications in system ROM:

Compiler filters:

  • Verify: verifies the DEX file only to verify the validity of each part.

  • Quicken: Optimize some DEX instructions based on Verify to improve interpreter performance.

  • Speed: AOT compilation of all methods based on Verify.

  • Speed-profile: AOT compilation of methods listed in the configuration file (hot methods) on the basis of Verify.

Default compilation mode of various applications in the system ROM:

  • Startup classpath code (part of the code used to start the system) : Compiles using the speed compilation filter by default

  • System server code (for example, power, multimedia service code) : It is compiled using the speed compilation filter by default

  • Product-specific core applications (such as the Ole services framework) : compile using the Speed-build filter by default

  • All third-party applications: compile using the Quicken build filter by default

At the beginning, all third-party applications only carry out dex checksum and some instruction optimization through Quiken mode. Vdex files are stored in dex codes after verification, so as to avoid repeated verification and speed up AOT compilation of hot codes.

conclusion

Pull so much light actually the core content is a sentence: App doesn’t compile the code only when installation check of legitimacy, the runtime through the interpreter, will run the code to be compiled into the memory cache frequently and recorded in the local configuration file, background threads compilation method of storage to the configuration file records. Odex file and run the App again read first. Odex file compiled code, and then repeat the process.