JVM是在不同平台上运行Java代码的虚拟机。它充当程序与运行Java代码的平台之间的抽象层。仅由于JVM,才可以移植Java代码。 javac编译器将源代码文件(Java文件)转换为独立于机器/平台的中间Java字节码格式。然后将此中间文件提供给目标机器/平台,在此将其转换为机器代码。 JVM支持多种主机体系结构,这就是Java应用程序被称为WORA(只要在任意位置写入一次)的原因。
DVM(Dalvik虚拟机)
DVM是执行Android应用程序的虚拟机。由javac编译器生成的Java字节码(.class文件)被转换为Dalvik字节码,以使应用程序源文件可在DVM上执行。由于Android设备具有确定的处理能力,内存和电池寿命,因此DVM设计原则旨在对其自身进行优化,以使其即使在低内存/低功耗设备上也可以快速加载并平稳运行。该虚拟机在同一设备上运行多个实例时非常高效。
差异表
JVM(Java Virtual Machine) |
DVM(Dalvik Virtual Machine) |
---|---|
Stack-based VM that performs arithmetic and logic operations through push and pop operands. The result of operations is stored in stack memory. | Register-based VM that uses registers located in the CPU to perform arithmetic and logic operations. |
Java source code is compiled into Java bytecode format(.class file) that further translates into machine code. | Source code files are first of all compiled into Java bytecode format like JVM. Further, the DEX compiler(dx tool) converts the Java bytecode into Dalvik bytecode(classes.dex) file that will be used to create the .apk file. |
More information is required to the VM for data loading and manipulation as well as method loading in the stack data structure. | Instruction size is larger as it needs to encode the source and destination register of the VM. |
Compiled bytecode size is compact because the location of the operand is implicitly on the operand stack. | Compiled bytecode size is larger as each instruction needs all implicit operands. |
The executable file for the device is .jar file. | The executable file for the device is .apk file. |
A single instance of JVM is configured with shared processes and memory space in order to run all deployed applications. | The device runs multiple DVM instances with a separate process in shared memory space to deploy the code of each application. |
Supports multiple operating systems like Linux, Windows, and Mac OS. | Support only the Android operation system. |
Note: Google introduced a new Virtual machine for android applications in 2014 which is known as Android Runtime(ART). It is designed with the purpose to improve application performance and reduce device battery usage. The last supported version for the DVM was Android 4.4(Kitkat).