What Is the Difference Between DVM and JVM? – DVM和JVM之间的区别是什么?

最后修改: 2020年 8月 17日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

In this article, we’ll explore the differences between the Java Virtual Machine (JVM) and the Dalvik Virtual Machine (DVM). We’ll first take a quick look at each of them and then make a comparison.

在本文中,我们将探讨Java 虚拟机(JVM)Dalvik 虚拟机(DVM)之间的区别。我们首先快速了解一下它们各自的情况,然后进行比较。

Note that starting with Android 5.0, the Dalvik Virtual Machine has been replaced by Android Runtime (ART).

请注意,从Android 5.0开始,Dalvik虚拟机已经被Android Runtime(ART)取代。

2. What Is a Runtime?

2.什么是运行时?

A runtime system provides an environment to translate the code written in a high-level language like Java to machine code and understandable by the Central Process Unit (CPU).

运行时系统提供了一个环境,以将用Java等高级语言编写的代码翻译成机器码,并让中央处理器(CPU)能够理解。

We can distinguish these types of translators:

我们可以区分这些类型的译员。

  • Assemblers: They directly translate assembly codes to machine code so it is fast
  • Compilers: They translate the code into assembly code, then it uses assemblers to translate the resulting code into binary. Using this technique is slow but the execution is fast. Also, the resulting machine code is platform dependent
  • Interpreters: They translate the code while executing it. Since the translation happens at runtime, the execution may be slow

3. Java Virtual Machine

3.Java虚拟机

The JVM is a virtual machine to run Java desktop, server, and web applications. Another important thing about Java is it was developed with portability in mind. Thus, the JVM has been shaped also to support multiple host architectures and run everywhere. But, it is too heavy for embedded devices.

JVM是一个虚拟机,用于运行Java桌面、服务器和Web应用程序。关于Java的另一个重要事项是它在开发时考虑到了可移植性。因此,JVM的形状也是为了支持多种主机架构并在各地运行。但是,它对嵌入式设备来说太重了。

Java has an active community and will continue to be widely used in the future. Moreover, HotSpot is the JVM reference implementation. As well, there are also more than five other implementations maintained by the Open Source Community.

Java拥有一个活跃的社区,并将在未来继续被广泛使用。此外,HotSpot是JVM的参考实现。此外,还有五个以上的其他实现由开源社区维护。

With the new cadence-based release, Java and the JVM are receiving new updates every six months. For example, we can list some proposals for the next releases such as Foreign-Memory Access and Packaging Tool.

随着新的基于cadence的发布,Java和JVM每六个月就会收到新的更新。例如,我们可以列出下一个版本的一些建议,如外国内存访问打包工具

4. Dalvik Virtual Machine

4.Dalvik虚拟机

The DVM is a virtual machine to run Android applications. The DVM executes Dalvik bytecode, which is compiled from programs written in the Java language. Note that the DVM is not a JVM.

DVM是一个运行Android应用程序的虚拟机。DVM执行Dalvik字节码,该字节码由以Java语言编写的程序编译而成。注意,DVM不是一个JVM。

One of the key design principles of the DVM is that it should run on low memory mobile devices and loads quicker compared to any JVM. Also, this VM is more efficient when it runs multiple instances on the same device.

DVM的关键设计原则之一是,它应该在低内存的移动设备上运行,并且与任何JVM相比,加载速度更快。另外,当它在同一设备上运行多个实例时,这种虚拟机的效率更高。

In 2014, Google released Android Runtime (ART) for Android 5 which replaced Dalvik for improved application performance battery usage. The last version was 1.6.0 on Android 4.4.

2014年,谷歌发布了Android Runtime(ART),用于Android 5,它取代了Dalvik,以改善应用程序的性能电池使用。最后一个版本是Android 4.4上的1.6.0。

5. Difference Between JVM and DVM

5.JVM和DVM之间的区别

5.1. Architecture

5.1.结构

The JVM is a stack-based VM where all the arithmetic and logic operations are carried out via push and pop operands and results are stored on the stack. The stack is also the data structure to store methods.

JVM是一个基于堆栈的虚拟机,所有的算术和逻辑操作都是通过推送和弹出操作数进行的,结果存储在堆栈中。堆栈也是用于存储方法的数据结构。

Contrastingly the DVM is a register-based VM. These registers located in the CPU carry out all the arithmetic and logic operations. The register is the data structure to store operands.

相比之下,DVM是一个基于寄存器的虚拟机。这些位于CPU中的寄存器执行所有的算术和逻辑操作。寄存器是用于存储操作数的数据结构。

5.2. Compilation

5.2.汇编

Java code is compiled inside the JVM to an intermediary format called Java bytecode (.class files). Then, the JVM parses the resulting Java bytecode and translates it to machine code.

Java代码在JVM内部被编译成一种叫做Java字节码(.class文件)的中间格式。然后,JVM 解析产生的Java字节码并将其翻译成机器代码

On an Android device, the DVM compiles the Java code to an intermediate format called Java bytecode (.class file) like the JVM. Then, with the help of a tool called Dalvik eXchange or dx, it transforms Java bytecode to Dalvik bytecode. Finally, the DVM translates the Dalvik bytecode to binary machine code.

在安卓设备上,DVM会像JVM一样将Java代码编译成一种叫做Java字节码(.class文件)的中间格式。然后,在一个名为Dalvik eXchange或dx的工具的帮助下,它将Java字节码转换为Dalvik字节码。最后,DVM将Dalvik字节码翻译成二进制机器代码

Both VMs use the Just-In-Time (JIT) Compiler. The JIT Compiler is a type of compiler that performs the compilation during the runtime.

两个虚拟机都使用Just-In-TimeJIT“/graal-java-jit-compiler”>)。编译器。JIT编译器是一种在运行时进行编译的编译器。

5.3. Performance

5.3.业绩

As seen previously, JVM is a Stack-based VM and DVM is a Register-based VM. Stack-based VM bytecode is very compact because the location of operands is implicitly on the operand stack. Register-based VM bytecode requires all the implicit operands to be part of an instruction. That indicates that the Register-based code size will usually be much larger than Stack-based bytecode.

如前所述,JVM是一个基于堆栈的虚拟机,DVM是一个基于寄存器的虚拟机。基于堆栈的虚拟机字节码非常紧凑,因为操作数的位置隐含在操作数堆栈中。基于寄存器的虚拟机字节码要求所有隐式操作数都是指令的一部分。这表明,基于寄存器的代码大小通常会比基于堆栈的字节码大得多。

On the other hand, register-based VM’s can express computations using fewer VM instructions than a corresponding stack-based VM. Dispatching a VM instruction is costly, so the reduction in executed VM instructions is likely to significantly improve the speed of the register-based VM.

另一方面,基于寄存器的虚拟机可以用比相应的基于堆栈的虚拟机更少的虚拟机指令来表达计算。派遣一条虚拟机指令的成本很高,所以减少执行的虚拟机指令可能会显著提高基于寄存器的虚拟机的速度

Of course, this distinction is only relevant when running the VM in interpreted mode.

当然,这种区别只有在解释模式下运行虚拟机时才有意义。

5.4. Execution

5.4.执行

Although it is possible to set up an instance of the JVM per running application, typically we’ll only configure one single instance of a JVM with shared processes and memory space to run all the applications we have deployed.

虽然可以为每个运行中的应用程序设置一个JVM实例,但通常情况下,我们只会配置一个具有共享进程和内存空间的JVM的单一实例来运行我们部署的所有应用程序。

However, Android was designed to run multiple DVM instances. So to run an application or a service, the Android OS creates a new DVM instance with a separate process in shared memory space and deploys the code to run the application.

然而,Android被设计为运行多个DVM实例。因此,为了运行一个应用程序或服务,Android操作系统在共享内存空间中用一个独立的进程创建了一个新的DVM实例,并部署代码以运行该应用程序。

6. Conclusion

6.结语

In this tutorial, we presented the main differences between the JVM and DVM. Both VM’s run applications written in Java, but they use different techniques and processes to compile and run code.

在本教程中,我们介绍了JVM和DVM的主要区别。两种虚拟机都运行用Java编写的应用程序,但它们使用不同的技术和流程来编译和运行代码。