Do you understand the relationship between ART, Dalvik and JVM?

Learn about virtual machines

JVM

JVM is the java virtual machine , which is the main way to realize the java platform. It can compile high-level languages ​​like java into machine language that can be recognized by the machine, so that java can be compiled once and run everywhere.
Run file format: .class file, based on stack

Dalvik

Dalvik is the virtual machine DVM that the Android system runs on . The Dalvik virtual machine is customized for mobile devices. It allows multiple instances of virtual machines to run in a limited memory at the same time, and each Dalvik application is an independent Linux process. The independent process can prevent all processes from being shut down when the virtual machine crashes. Dalvik is an engine based on JIT (Just in time) compilation.
[Supplement]: Dalvik is a virtual machine developed and designed for the Android platform by Google and other manufacturers. It is one of the core components of the Android mobile device platform.
Run file format: Java application in .dex format, based on registers

ART

ART is a managed runtime environment (Android Runtime) used by applications and some system services on Android. ART was launched after Android 4.4 KitKat. Starting from Android 5.0 Lollipop, ART replaces the Dalvik virtual machine by default, and ART is also fully compatible with Dalvik. The virtual machine uses AOT (Ahead of time) compilation . After 7.0, the system uses a combination of AOT, just-in-time (JIT) compilation and configuration file guided compilation, a hybrid mode.
Run file format: Java application in .dex format, based on registers

Just In Time (JIT)

Each time an application in the DVM runs, a part of the Dalvik bytecode is translated into machine code (native execution) in real time through JIT. During the execution of the program, the .dex is continuously compiled and cached in advance.
Advantages : consumes less memory and takes up less physical storage space.
Disadvantages : reduces the operating efficiency of the application

Ahead-Of Time (AOT)

The ART system will perform a pre-compilation (Ahead Of Time, AOT) when the application is installed for the first time (only occurs when the application is installed) , and the .dex bytecode file is pre-compiled into machine code (native execution)** and There is a local, so the application does not need to be compiled every time it runs, which can greatly improve the operating efficiency.
Advantages : 1. Take up less CPU resources and consume less power; 2. Reduce startup time; 3. No need for instant translation, which can save power; 4: Improve garbage collector; 5. Application runs faster, directly execute machine code , The .dex file has been translated during installation.
Disadvantages : 1. AOT is required for installation, which takes longer to install; 2. Additional space is required to store the compiled machine code

Related terms

.dex file

The .dex format is a compression format designed specifically for Dalvik, suitable for systems with limited memory and processor speed.

In Android, after .java is compiled into .class files, multiple class files will be packaged into class.dex by related tools and placed in apk. For example, Android studio can use the dx command of the SDK to package the specified .class as .dex.

Assembler

It directly translates assembly language into machine code, so its speed is very fast.

translater

It translates the source code into assembly language, and then uses the assembler to convert it into machine code. The compilation process in this way is slow but the execution speed is fast. But the biggest problem with using a compiler is that the compiled machine code depends on a specific platform. In other words, code that can run on one machine may not run on a different machine.

Interpreter

It translates the code when the program is executed. Since the code translation only occurs during the execution phase, the execution speed is very slow.

Multidex

Before android5.0, each android application contained only one dex file, and the number of dex methods was limited to 65535. As a result, apk introduced a large number of third-party SDKs and the number of methods exceeded the limit and could not be compiled. In order to solve this problem, Google launched the multidex file solution multidex, an apk can contain multiple dex files. Complete the loading of the dex file through Multidex.install(this).

ART was introduced in Android 4.4. After 5.0, the default virtual machine of the terminal selects ART. ART supports multiple .dex files.

Supplement

ART compatibility with Dalvik

ART使用设备自带的dex2oat工具来编译应用。此工具将dex文件编译为应用可执行文件。 但是一些后处理工具会生成无效文件,Dalvik可以接受这些文件,但是ART无法编译这些文件。所以ART只是基本上兼容Dalvik,不是不完全。

ART's optimization measures for garbage collection

  1. Only once (not twice) GC pause
  2. Parallel processing while the GC remains paused
  3. In the special case of cleaning up recently allocated short-term objects, the total GC time of the collector is shorter
  4. The ergonomics of garbage collection is optimized, and parallel garbage collection can be performed more timely, which makes GC_FOR_ALLOC event extremely rare in typical use cases
  5. Compress GC to reduce background memory usage and fragmentation

Why does Android use a virtual machine?

Android uses a virtual machine as its running environment to run Android applications composed of APK files.

  1. The application code is separated from the core operating system. Therefore, even if any program contains malicious code, it will not directly affect system files. This makes the Android operating system more stable and reliable.
  2. It improves cross-platform compatibility or platform independence. This means that even if an application is compiled on a PC, it can be executed on a mobile platform through a virtual machine.

Comparison of Dalvik and JVM

  1. Dalvik takes up less space;
  2. To simplify translation, the constant pool only uses 32-bit indexes; 3. Standard Java bytecode implements 8-bit stack instructions, and Dalvik uses 16-bit instruction sets to directly act on local variables. Local variables usually come from the 4-bit "virtual register" area. This reduces Dalvik's instruction count and improves translation speed.
  3. Generally speaking, stack-based machines must use instructions to load and manipulate data from the stack. Therefore, compared to register-based machines, they require more instructions to achieve the same performance. But instructions on register-based machines must be coded, so their instructions are often larger.
  4. The Dalvik virtual machine neither supports Java SE nor Java ME class libraries (such as Java classes, neither AWT nor Swing support). Instead, it uses its own class library (a subset of Apache Harmony Java).

Dalvik's partial mechanism principle

  1. When Android starts, Dalvik VM monitors all programs (APK) and creates a dependency tree, optimizes the code for each program and stores it in the Dalvik cache. Dalvik will generate a Cache file after loading it for the first time to provide fast loading next time, so opening the program for the first time will be very slow.
  2. The Dalvik interpreter uses a pre-calculated Goto address, and each instruction's memory access is aligned on a 64-byte boundary. This can save the time of looking up the table after an instruction. In order to enhance the function, Dalvik also provides a fast interpreter (Fast Interpreter).

After Android 7.0, a mixed execution plan of interpretation, JIT, and AOT is adopted

In Android N, its ART has been greatly changed. The main reason is that the code of the same program may run at the same time in the mixed operation mode of local machine code (compilation), interpretation and JIT (Just In Time), and different users and the code of the same application may run different compiled codes. Because of Profile-guided JIT/AOT Compilation, different user behaviors may have different compilation results for the same app.N 上做此变化的其目的是为了在安装时间、内存占用、电池消耗和性能之间获得最好的折衷。

**The new solution is: **When the device is idle and charging, ART will perform AOT 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 strategy of mixing AOT, interpretation, and JIT has the following advantages over pure AOT or JIT:

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

Comparison diagram of JVM, DVM, ART

`Picture reference Tang Xian-seng

Please add picture description

该篇博客纯属个人观点和见解,如有错误恳请留言指正,万分感激!Please add picture description
该篇博客纯属个人观点和见解,如有错误恳请留言指正,万分感激!
Reference link:
https://www.jianshu.com/p/bdb6c29aca83

Guess you like

Origin blog.csdn.net/luo_boke/article/details/106004778