【Android】Android virtual machine

virtual machine

There are two main types of Android virtual machines: Dalvik 虚拟机and ART(Android Runtime)虚拟机.

Dalvik 虚拟机
The Dalvik virtual machine is a virtual machine used in the early days of Android, it 基于寄存器架构. Starting from Android 2.2, it supports JIT即时编译(Just In Time)selecting hot codes (codes that are often executed) for compilation or optimization during the running of the program. Dalvik executes converted and optimized dex (Dalvik Executable) bytecode files instead of traditional Java bytecodes. The Dalvik virtual machine is optimized for the characteristics of mobile devices, with a small memory footprint and high execution efficiency. Each application runs in an independent instance of the Dalvik virtual machine, achieving isolation between applications.

ART 虚拟机
Since Android 5.0 (Lollipop), Android has introduced the ART virtual machine as the default operating environment. ART uses Ahead-Of-Time (AOT) compilation technology to pre-compile dex bytecode into local machine code when the application is installed, so that real-time bytecode conversion is not required at runtime, which improves the startup of the application. Speed ​​and execution efficiency. Unlike Dalvik, ART's implementation is closer to that of a traditional Java virtual machine, using 基于堆栈的指令集.

the difference

基于寄存器的虚拟机

In a register-based virtual machine, the 数据保存在寄存器. Virtual registers can be seen as one 数组, used to store runtime data such as local variables, parameters, and temporary variables. Each virtual register has its own number, and the value in the register can be accessed and manipulated through the number.

In Dalvik VM, each thread has its own 程序计数器(Program Counter, PC) and 调用栈. The program counter is used to record the instruction position executed by the current thread, and the call stack is used to save the activity record of the method call, and each activity record is called a frame. Each frame contains information such as the method's local variable table and operand registers.

Therefore, for a register-based virtual machine, data is stored in registers, and the virtual register is an array that is stored on the runtime stack. Each thread has its own program counter and call stack, and the activity record of method calls is stored on the call stack in units of frames.

基于栈的虚拟机

Each virtual machine runtime has its own thread 独立的栈. The stack is used to record the history of method calls, and whenever one is 方法被调用called, one will be created on the stack 新的栈帧(stack frame). The stack frame contains the method's local variable table, operand stack, and some additional information.

The top stack frame of the stack is called 当前栈帧, for 当前正在执行的方法. The local variable table in the stack frame is used to store the local variables and parameters defined in the method, and the operand stack is used to execute the operations in the method.

A stack-based virtual machine 操作数栈performs all instruction operations. Instructions can fetch operands from the operand stack, perform the corresponding operation, and then push the result back onto the operand stack. This stack-based instruction set design is simple and compact, and does not depend on a specific hardware architecture, so it can achieve better cross-platform performance.

When 方法执行完成or when a method call is encountered, the current 栈帧会被弹出, and 恢复到上一个栈帧, continue to execute the previous method. In this way, by continuously creating and destroying stack frames, a stack-based virtual machine can realize seamless switching between methods and management of control flow.

To sum up, a stack-based virtual machine uses an independent stack to record the method call history, and each stack frame represents the execution context of a method, including the local variable table and the operand stack. Instruction operations are performed through the operand stack to realize switching between methods and control flow management. This design has the characteristics of simplicity, compactness and cross-platform.

Android program installation optimization

When an application is installed on the Android system, different optimization processes are performed depending on the runtime environment (Dalvik or ART) used.

In Dalvik 虚拟机, the application will be optimized once during the installation process, which will dex 字节码convert the application into an optimized executable file odex(Optimized DEX)文件. This optimization process dexopt is done by the tool. dexopt performs operations such as bytecode optimization, pre-parsing, and pre-verification on dex files according to device configuration and system policies, and generates odex files. The odex file contains optimized bytecode and related metadata information, which accelerates the loading and execution speed of the application runtime.

And in ART(Android Runtime), introduced 预先编译机制(Ahead Of Time Compilation). When the application is installed, ART uses the built-in dex2oattools of the device to directly compile the application's dex bytecode into local machine code, that is, convert the application's dex file to ELF(Executable and Linkable Format)可执行文件. 应用安装期间Depending on the device and system policy, this compilation process can happen either at or when the app is first run.

By compiling dex bytecode into native machine code, ART provides higher application execution performance and lower memory footprint. 与 Dalvik 不同,ART 在应用运行时无需进行即时编译(Just-In-Time Compilation),而是直接执行本地机器码, improving the response speed and efficiency of the application.

insert image description here

Guess you like

Origin blog.csdn.net/qq_43358469/article/details/131479841