dvm与art的区别_Android运行时– DVM与ART,AOT与JIT

dvm与art的区别

In this tutorial, we’ll be discussing the runtime environments used in Android. We’ll see the differences and how it is improving for the betterment of android applications execution.

在本教程中,我们将讨论Android中使用的运行时环境。 我们将看到差异以及它如何改善Android应用程序的执行。

运行环境 (Runtime Environment)

Runtime Environment consists of software instructions(generated from the code) that are used while executing the programming.
JVM is the component that is used to convert bytecode into machine code in order to run Java-based programs.

运行时环境由在执行编程时使用的软件指令(从代码生成)组成。
JVM是用于将字节码转换为机器代码以便运行基于Java的程序的组件。

Why is a Virtual Machine need to run any Application?

为什么虚拟机需要运行任何应用程序?

  • A Virtual Machine isolates the execution of the program from the OS. Thus protecting malicious code from affecting the system files.

    虚拟机将程序的执行与OS隔离开。 从而防止恶意代码影响系统文件。
  • Virtual Machines execute code independent of the CPU architecture

    虚拟机执行与CPU架构无关的代码

Dalvik Virtual Machine (DVM) was specifically designed to run Android applications initially.

Dalvik虚拟机(DVM)专为最初运行Android应用程序而设计。

Why Android uses DVM and not JVM? 为什么Android使用DVM而不使用JVM?

Mobile Environment is not as powerful as your computer systems(majorly). There are battery and ram constraints. DVM was specifically optimized in order to run on Android.

移动环境不如您的计算机系统强大(主要是)。 有电池和内存限制。 DVM经过特别优化,可以在Android上运行。

JVM uses a Stack-based model.
DM uses a Register based model.

JVM使用基于堆栈的模型。
DM使用基于寄存器的模型。

基于堆栈的模型与基于寄存器的模型之间的区别 (Difference between Stack-based and Register-based Models)

Stack-based Virtual Machines have the memory structure of the type Stack.
Register-based uses registers of the CPU to store the operands. So along with the operands, their address is also stored.

基于堆栈的虚拟机具有堆栈类型的内存结构。
基于寄存器的使用CPU的寄存器来存储操作数。 因此,还与操作数一起存储了它们的地址。

A Register based model does not require any pushing and popping of instructions. Hence instructions execute faster.

基于寄存器的模型不需要任何推送和弹出指令。 因此,指令执行速度更快。

Register-based models are good at optimizing as well. They can store common subexpression results which can be used again in the future without the need to calculate. This is not possible in a Stack-based model.

基于寄存器的模型也擅长优化。 它们可以存储常见的子表达式结果,以后无需计算即可再次使用。 这在基于堆栈的模型中是不可能的。

Android DVM (Android DVM)

Before looking at the Dalvik Virtual Machine, let’s look at the JVM:

在查看Dalvik虚拟机之前,让我们看一下JVM:

Following is the flow of JVM:

以下是JVM的流程:

Following is the flow of DVM:

以下是DVM的流程:

We know that DVM is specifically used for low memory devices.

我们知道DVM专用于低内存设备。

The Dex compiler (dx tool) converts the .class files generated from the javac compiler to .dex file.
These .dex files are then converted to machine code.

Dex编译器( dx工具)将从javac编译器生成的.class文件转换为.dex文件。
然后,这些.dex文件将转换为机器代码。

Note: dexopt tool which is a part of the DVM converts the dex file into .odex file format.

:dexopt工具,它是DVM的一部分转换DEX文件到.odex文件格式。

JVM dynamically loads the bytecode for each class from the corresponding .class file. While Dalvik bytecode is only composed of one .dex file, containing all the classes of the application.

JVM从相应的.class文件动态加载每个类的字节码。 Dalvik字节码仅由一个.dex文件组成,包含应用程序的所有类。

How is the dex bytecode converted into machine code?

dex字节码如何转换为机器码?

Using JIT(Just In Time).

使用JIT (及时)。

Just In Time is a component that takes application code, analyzes it, and actively translates it into a form that runs faster, doing so while the application continues to run. This leads to increased launch time for applications since it needs to be done everytime the application is launched.

准时制是一个组件,它接收应用程序代码,对其进行分析,然后将其主动转换为运行速度更快的形式,并在应用程序继续运行时这样做。 由于需要在每次启动应用程序时完成,因此这会增加应用程序的启动时间。

As the execution progresses, more bytecode is compiled and cached. This leads to faster boot times.

随着执行的进行,更多的字节码将被编译和缓存。 这样可以缩短启动时间。

DVM and JIT were replaced by ART and AOT respectively since Android Lollipop.

自Android Lollipop以来,DVM和JIT分别被ART和AOT取代。



dx tool was responsible for it. dx工具对此负责。

Since Android Studio 3.1.0, D8 tool has released. It goes a step ahead of dx and builds faster and the .dex file generated would be smaller. Thus APK size would be smaller.
Set android.enableD8=true in gradle.properties.

自Android Studio 3.1.0起,D8工具已发布。 它比dx领先一步,构建速度更快,生成的.dex文件更小。 因此APK大小会更小。
在gradle.properties中设置android.enableD8=true

Since Android Studio 3.2.0 R8 tool has been released as a code shrinker. It’s a step ahead of Proguard and is used to convert Java bytecode to dex whilst shrinking and obfuscating code.

自Android Studio 3.2.0 R8工具发布以来,它已成为代码收缩器。 它比Proguard领先一步,可用于在缩小和混淆代码的同时将Java字节码转换为dex。

Set android.enableR8=true in gradle.properties.

在gradle.properties中设置android.enableR8=true

Android ART (Android ART)

Android Runtime has replaced DVM since Android Lollipop. ART uses Ahead of Time Approach (AOT) instead of JIT.

自Android Lollipop以来,Android运行时已取代DVM。 ART使用提前方式(AOT)代替JIT。

Using AOT, the dex files are compiled before they are needed. Usually, they are done at installation time only and then stored in phone storage.

使用AOT,可以在需要之前编译dex文件。 通常,它们仅在安装时完成,然后存储在手机存储中。

Let’s look at the change in flow using ART:

让我们看一下使用ART的流程变化:

ART与DVM (ART vs DVM)

ART uses AOT based approach and DVM uses JIT.

ART使用基于AOT的方法,而DVM使用JIT。

The only drawbacks of ART :
Increased installation time, increased reboot time, increased storage.

ART的唯一缺点:
增加了安装时间,增加了重新启动时间,增加了存储量。

  • Battery Performance: ART largely increases the battery performance compared to DVM since the dex bytecode is not interpreted every time.

    电池性能 :与DVM相比,ART大大提高了电池性能,因为不会每次都解释dex字节码。
  • Storage : On installation, the AOT stores the precompiled binary in phone storage directly. Hence DVM is more suited for phones with low storage.

    存储 :安装后,AOT将预编译的二进制文件直接存储在电话存储中。 因此,DVM更适合于存储空间低的手机。
  • Boot Time: ART causes a slower reboot time since the cache is built at first boot, hence rebooting device takes significantly longer.

    引导时间 :ART会导致重新引导时间变慢,因为高速缓存是在首次引导时建立的,因此重新引导设备花费的时间明显更长。
  • Application Launch Time: DVM has a slower launch time for the applications. ART has the native code ready to execute thanks to AOT. Hence is super fast.

    应用程序启动时间 :DVM的应用程序启动时间较慢。 借助AOT,ART可以立即执行本机代码。 因此是超级快。
  • Garbage Collection: ART has much better Garbage collection than DVM. In DVM the heap is more fragmented. Whereas ART uses a different heap for storing large objects such as Bitmaps. Moreover, ART has a concurrent compacting Garbage collector and is able to compact the heap easier compared to DVM

    垃圾收集 :ART的垃圾收集比DVM好得多。 在DVM中,堆更加分散。 而ART使用不同的堆来存储大对象(例如位图)。 此外,ART具有并发压缩垃圾收集器,并且与DVM相比,能够更轻松地压缩堆

重新引入JIT (Reintroducing JIT)

Android Nougat reintroduced Just In Time Compilation with code profiling along with AOT, and an interpreter in the ART thus making it hybrid.
This is done in order to tackle problems such as initial installation time and memory.
Using the Hybrid Runtime, there won’t be any compilation during install, and applications can be started right away, the bytecode is interpreted.
Now with ART and the new JIT the application installation is faster.

Android Nougat重新引入了带有代码概要分析的即时编译功能以及AOT,并在ART中提供了解释器,从而使其具有混合性。
这样做是为了解决诸如初始安装时间和内存之类的问题。
使用Hybrid Runtime,在安装过程中不会进行任何编译,并且可以立即启动应用程序,并解释字节码。
现在,借助ART和新的JIT ,应用程序安装变得更快。

The new JIT constantly does profiling and improves applications as they run.

新的JIT会不断进行性能分析并在应用程序运行时对其进行改进。

In Android P ART, Kotlin optimisations are also enhanced.
在Android P ART中,Kotlin优化也得到了增强。

That brings an end to this tutorial.

这样就结束了本教程。

翻译自: https://www.journaldev.com/23464/android-runtime-dvm-vs-art-aot-vs-jit

dvm与art的区别

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值