JVM review---DVM/ART

This part is the content of Chapter 11 in Advanced Decryption

Dalvik virtual machine

Referred to as DVM, it is a virtual machine developed by Google specifically for the Android platform. It runs in the Android runtime library. But it is not a java virtual machine, this is mainly because DVM does not follow the JVM specification to achieve.

The difference between DVM and JVM

  • Based on different architectures

JVM is stack- based, which means that data needs to be read and written from the stack, which requires more instructions and slows down the speed ;

The DVM is register- based, it does not have a large number of stack-in/out instructions used by stack-based virtual machines when copying data, and the instructions are more compact and concise.

  • Different bytecodes executed

In the java SE program, the java class is compiled into one or more .class files and packaged into a .jar file, and then the JVM will obtain the corresponding bytecode through the corresponding .class file and .jar file . The .jar file contains one or more .class files, and each .class file contains the constant pool, class information, attributes, etc. of the class. When the JVM loads the .jar file, it will load all the .class files in it, which is very slow.

The DVM will convert all the .class files into a .dex file through the dx tool, and then read the instructions and data from the .dex file . In the .apk file, only one .dex file is included. This file integrates all the information of the .class files, so that reloading speeds up. There are a lot of redundant information in the .class file. The dex tool will remove the redundant information and integrate all the .class files into the .dex file, reducing I/O operations and improving the search speed of classes.

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-aux598o7-1615815409422)(https://s3-us-west-2.amazonaws.com/secure.notion -static.com/234f68be-59ad-424e-a620-c1afa9f65345/Untitled.png)]

  • DVM allows multiple processes to run simultaneously in limited memory

Each application in Android runs in a DVM instance, and each DVM instance runs in an independent process space, which can prevent all programs from being closed when the VM crashes.

  • DVM is created and initialized by the Zygote process
  • DVM has a sharing mechanism

DVM has a pre-loading-sharing mechanism, and different applications can share the same class at runtime, which has higher efficiency. (Zygote Space area can be shared between Zygote process and application process)

  • DVM did not use the JIT compiler in the early days

JVM uses JIT (Just In Time Compiler, just-in-time compiler), while DVM did not use JIT compiler in the early days. Every time the code is executed, the dex code needs to be compiled into machine code and then executed, which is inefficient. JIT can compile hot codes that have been run multiple times to generate a fairly streamlined local machine code, which can be used directly when the same logic is executed next time. But the application needs to be re-JIT-compiled every time it is re-run.

DVM runtime heap

GC algorithm

DVM runtime heap uses mark-sweep algorithm for GC

The composition of the pile

It consists of two Spaces and multiple auxiliary data structures .

The two Spaces are Zygote Space (Zygote Heap) and Allocation Space (Active Heap).

Zygote Space : Used to manage the objects preloaded and created during the startup of the Zygote process. It will not trigger GC, and the Zygote Space will be shared between the Zygote process and the application process.

**Allocation Space: **Before the Zygote process forks the first child process, the Zygote Space will be divided into two parts. The part that has been used is still called Zygote Space, and the unused part is called Allocation Space. , Future objects (should be the objects of the newly created process) will be allocated and released in Allocation Space. Allocation Space is not shared by processes, each process has an independent copy.

Auxiliary data structure:

  • Card Table: Concurrent GC used for DVM, when the garbage mark is performed for the first time, the garbage information is recorded.
  • Heap Bitmap: There are two Heap Bitmaps, which are used to record the objects that are still alive after the last and this GC.
  • Mark Stack: Used to traverse the surviving objects in the GC marking phase.

ART

ART was released in Android 4.4 to replace DVM, and ART will be adopted by default after 5.0.

The difference between ART and DVM

  • DVM is designed for 32-bit CPU, while ART supports 64-bit and is compatible with 32-bit CPU . This is one of the main reasons why DVM is eliminated.
  • ART has improved the GC mechanism , such as: performing parallel GC more frequently, reducing GC pauses from 2 to 1 and so on.
  • ART's runtime heap space division is different from DVM.
  • ART will perform an AOT (pre-compilation) when installing the application to pre-compile the bytecode into machine code and store it locally, so that there is no need to compile each time the program is run, which improves the efficiency of the program. But this also has disadvantages : **1.**AOT will make the program installation time longer. **2.** Pre-compilation of bytecode into machine code requires additional space. In order to solve these shortcomings, the JIT just-in-time compiler is added to the ART of Android7.0. When the application is installed, the bytecode is not compiled into machine code, but the hot code is compiled into machine code at runtime, which is also regarded as A compromise approach.

Guess you like

Origin blog.csdn.net/why1092576787/article/details/114853359
JVM
JVM