java DVM 和 ART 有什么区别?为什么DVM在Lollipop中正式被ART取代了?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31957568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:24:47  来源:igfitidea点击:

What is difference between DVM and ART ? Why DVM has been officially replaced with ART in Lollipop?

javaandroiddalvik

提问by Amit Prajapati

Since Android Lollipop, Google has replaced DVM with ART because ART is faster than DVM. Is there any other reason for that?

从 Android Lollipop 开始,Google 就用 ART 取代了 DVM,因为 ART 比 DVM 快。还有其他原因吗?

回答by Aritra Roy

There are some major performance improvements that ART bringswhich were lacking in Dalvik. But every pros have some cons too. I will try to discuss both the advantages and disadvantages here.

ART 带来了一些Dalvik 所缺乏的重大性能改进。但每个优点也有一些缺点。我将尝试在这里讨论优点和缺点。



1) Compilation Approach

1) 编译方式

This is by far the biggest advantage of ART over Dalvik. The old guy Dalvik used Just-In-Time (JIT) approachin which the compilation was done on demand. All the dex files were converted into their respective native representations only when it was needed.

这是迄今为止 ART 相对于 Dalvik 的最大优势。老家伙 Dalvik 使用即时 (JIT) 方法,其中编译是按需完成的。只有在需要时,所有 dex 文件才被转换为它们各自的原生表示。

But ART uses the Ahead-Of-Time (AOT) approach, in which the dex files were compiled before they were demanded. This itself massively improves the performance and battery life of any Android device.

但是ART 使用 Ahead-Of-Time (AOT) 方法,其中 dex 文件在被要求之前被编译。这本身极大地提高了任何 Android 设备的性能和电池寿命。

For example

例如

In case of Dalvik, whenever you touch an app icon to open it, the necessary dex files gets converted into their equivalent native codes. The app will only start working when this compilation is done. So, the app is unresponsive until this finishes.

在 Dalvik 的情况下,每当您触摸应用程序图标将其打开时,必要的 dex 文件都会转换为等效的本机代码。该应用程序将仅在此编译完成后开始工作。因此,该应用程序在完成之前没有响应。

Moreover, this process is repeated every single time you open an app wasting CPU cycles and valuable battery juice.

此外,每次打开应用程序时都会重复此过程,从而浪费 CPU 周期和宝贵的电池电量。

But in case of ART, whenever you install an app, all the dex files gets converted once and for all. So the installation takes some time and the app takes more space than in Dalvik, but the performance is massively improved and battery life is smartly conserved.

但在 ART 的情况下,每当您安装应用程序时,所有 dex 文件都会一劳永逸地转换。因此安装需要一些时间,应用程序比 Dalvik 占用更多空间,但性能大大提高,电池寿命也得到了巧妙的节省



2) Boot Time

2) 开机时间

In case of Dalvik, the cache is built with time the device runs and apps are used as is indicated by the JIT approach. So the boot time is very fast.

在 Dalvik 的情况下,缓存是随着设备运行和应用程序的使用而构建的,正如 JIT 方法所指示的那样。所以开机时间非常快。

But in case of ART, the cache is built during the first boot, so the boot time is considerably more in case of ART. You might see an "Optimizing apps" dialog box sometimes you boot.

但是在 ART 的情况下,缓存是在第一次启动时构建的,因此在 ART 的情况下启动时间要长得多。有时您可能会在启动时看到“优化应用程序”对话框。



3) Space Usage

3) 空间使用

The space used by apps being run on ART is much more than that of Dalvik. Like a 20 MB app on Dalvik, takes more than 35 MB on ART.

在 ART 上运行的应用程序使用的空间比 Dalvik 多得多。就像 Dalvik 上的 20 MB 应用程序,在 ART 上占用超过 35 MB。

So if you are on a low storage device, then this can be a huge disadvantagefor you.

因此,如果您使用的是低存储设备,那么这对您来说可能是一个巨大的劣势



4) ART is Damn Fast

4) ART 太快了

As discussed above, ART is extremely fast and smooth. Apps are very snappy and responsive. Any comparison between Dalvik and ART, will surely make the ART device win by a significant margin.

如上所述,ART 非常快速和流畅。应用程序非常活泼且响应迅速。Dalvik 和 ART 之间的任何比较,肯定会让 ART 设备以显着优势获胜。

ARTis the answer to all those who argued that iOS is faster and smoother than Android and is also more battery efficient.

对于那些认为 iOS 比 Android 更快、更流畅并且电池效率更高的人,ART是他们的答案。

回答by kazisami

In Android Lolipop, DVM has been replaced by ART. Because DVM converts bytecode everytime you launch a specific app. But ART converts it just once at the time of app installation. That makes CPU execution easier. ARM is a architecture. Like MIPS, x86 etc. DVM/ART runs on top of ARM. Both of them can not replace each other. I hope this clears your confusion.

在 Android Lolipop 中,DVM 已被 ART 取代。因为每次启动特定应用程序时,DVM 都会转换字节码。但是 ART 在应用程序安装时只转换一次。这使得 CPU 执行更容易。ARM 是一种架构。像 MIPS、x86 等。 DVM/ART 运行在 ARM 之上。两者都不能互相替代。我希望这能消除你的困惑。

回答by Ayusch

Here is a post covering ART vs DVM in-depth. https://ayusch.com/art-vs-dvm-in-android/

这是一篇深入介绍 ART 与 DVM 的文章。https://ayusch.com/art-vs-dvm-in-android/

It dives deeper into difference between stackvs registerbased architecture (DVM is register based) and also how AOT compilation has made ART much faster than DVM by compiling the code to be executed beforehand.

它深入探讨了堆栈与基于寄存器的架构(DVM 是基于寄存器的)之间的区别,以及 AOT 编译如何通过预先编译要执行的代码使 ART 比 DVM 快得多。

回答by Dhaval Jivani

Android 4.4 KitKat, Google decided to introduce a new way of executing apps on top of the Android operating system. Let's take a closer look at what this new runtime, called ART

在 Android 4.4 KitKat 中,Google 决定引入一种在 Android 操作系统之上执行应用程序的新方式。让我们仔细看看这个名为ART 的新运行时

ART is Google's 2-year-long ongoing secret project, which aims to boost the performance of our Android devices.

ART 是谷歌持续两年的秘密项目,旨在提升我们 Android 设备的性能。

Android L (5.0) ART has been made as the default runtime (ART has completely replaced Dalvik).

Android L (5.0) ART 已成为默认运行时(ART 已完全取代 Dalvik)。

ART (Android RunTime)is the next version of Dalvik. Dalvik is the runtime, bytecode, and VM used by the Android system for running Android applications.

ART (Android RunTime)是 Dalvik 的下一个版本。Dalvik 是 Android 系统用于运行 Android 应用程序的运行时、字节码和 VM。

Dalvik is based on JIT (just in time)compilation. It means that each time you run an app, the part of the code required for its execution is going to be translated (compiled) to machine code at that moment. As you progress through the app, additional code is going to be compiled and cached, so that the system can reuse the code while the app is running. Since JIT compiles only a part of the code, it has a smaller memory footprint and uses less physical space on the device.

Dalvik 基于 JIT(即时)编译。这意味着每次运行应用程序时,其执行所需的部分代码将在那一刻被翻译(编译)为机器代码。随着应用程序的进行,额外的代码将被编译和缓存,以便系统可以在应用程序运行时重用这些代码。由于 JIT 仅编译部分代码,因此它具有更小的内存占用和更少的设备物理空间。

ART vs Dalvik / AOT vs JIT

ART vs Dalvik / AOT vs JIT

Advantages of ART over Dalvik:

ART 相对于 Dalvik 的优势:

  1. The apps launch speed is amazingly fast in case of ART since nothing is compiled at execution.
  2. Boot speed is faster than dalvik since nothing is execued from dalvik partition as in case of odexed ROM in dalvik
  3. Increases battery backup by reducing CPU work due to absence of compilation work on apps execution.
  4. Improved Garbage Collection (GC)
  5. And finally it is a great reward to developers because most of developers are worried that odexed ROMs are faster than deodexed ROMs but they will deodex their ROMs since they heavily theme it. In case of ART whether the ROM is odexed or deodexed it doesn't matter, it compiles the full code into machine language on installation of apps. so even deodexed ROMs are as fast as odexed in ART
  1. 在 ART 的情况下,应用程序的启动速度非常快,因为在执行时没有编译任何内容。
  2. 启动速度比 dalvik 快,因为 dalvik 分区没有执行任何操作,就像 dalvik 中的 odexed ROM
  3. 由于应用程序执行时没有编译工作,因此通过减少 CPU 工作来增加电池备份。
  4. 改进的垃圾收集 (GC)
  5. 最后,这对开发人员来说是一个巨大的回报,因为大多数开发人员担心 odexed ROM 比 deodexed ROM 更快,但他们会 deodex 他们的 ROM,因为它们的主题很重。在 ART 的情况下,ROM 是 odexed 还是 deodexed 无关紧要,它会在安装应用程序时将完整代码编译成机器语言。所以即使是 deodexed ROM 也和 ART 中的 odexed 一样快

Disadvantages of ART

ART的缺点

  1. Since ART precompiles apps on installation, it takes 10-20% more space upon installation than dalvik.
  2. As dex bytecodes are converted to native machine code on installation itself, installation takes more time.
  1. 由于 ART 在安装时预编译应用程序,因此它在安装时比 dalvik 多占用 10-20% 的空间。
  2. 由于 dex 字节码在安装时被转换为本地机器码,因此安装需要更多时间。

Install times on my Nexus 4, for one of our larger projects, jumped from ~17 s to ~25 s.

在我的 Nexus 4 上,对于我们一个较大的项目,安装时间从约 17 秒跃升至约 25 秒。

Dalvik vs. ART Benchmark Results (Android 4.4) : Linpack

Dalvik 与 ART 基准测试结果(Android 4.4):Linpack

Single Thread ---> Dalvik (135)---> ART(149)---> 10.93%

单线程 ---> Dalvik (135)---> ART(149)---> 10.93%

Multi- Thread ---> Dalvik (336)---> ART(383)---> 13.82%

多线程 ---> Dalvik (336)---> ART(383)---> 13.82%

回答by Kamal Kakkar

In Android 4.4 and above version, along with Dalvik, Google introduced a new Android Runtime called “ART”.

在 Android 4.4 及以上版本中,Google 与 Dalvik 一起推出了一种名为“ART”的全新 Android Runtime。

Difference between ART vs Dalvik

ART 与 Dalvik 的区别

ART (Android RunTime) has main feature Ahead-of-Time (AOT) compilation,

ART (Android RunTime) 的主要功能是 Ahead-of-Time (AOT) 编译,

AOT vs JIT

AOT 与 JIT

Android apps format is .apk and all Java classes converted into DEX bytecode. The DEX bytecode format is independent of device architecture and needs to be translated to native machine code to run on the device. This is the same for both the ART and the Dalvik runtimes.

Android 应用程序格式为 .apk,所有 Java 类都转换为 DEX 字节码。DEX 字节码格式独立于设备架构,需要转换为本地机器码才能在设备上运行。这对于 ART 和 Dalvik 运行时都是一样的。

Main Difference, ART (Android RunTime) used Ahead-of-Time (AOT) compilation,Dalvik is used Just-in-Time (JIT) compilation

主要区别,ART(Android RunTime)使用Ahead-of-Time(AOT)编译,Dalvik使用Just-in-Time(JIT)编译

With Dalvik ,Just-in-Time (JIT) compilation every Time when app run ,It convert dex byte code into machine code and cached.

使用 Dalvik,每次应用程序运行时,Just-in-Time (JIT) 编译,它将 dex 字节码转换为机器码并缓存。

see here more detail http://androidboost.over-blog.com/2016/10/art-vs-dalvik.html

在这里看到更多细节 http://androidboost.over-blog.com/2016/10/art-vs-dalvik.html

回答by Cevin Ways

Since kitkat (Android version 4.4), Google has been replace the run time DVMto ART. Google do this because it will improve the performance while the application launched. in DVM while you click the app, it will compile the bytecode DEX and convert it to machine code, usually called JIT (Just In Time). Instead of running the compile process every time application launch, ART has changed that way by convert the bytecode DEX and convert it to machine code when application in installation step. This might be take a long time while you install the app, but it will be faster enough when you launch the app.

从 kitkat(Android 4.4 版)开始,Google 已经将运行时DVM替换为ART。Google 这样做是因为它会在应用程序启动时提高性能。在 DVM 中,当您单击应用程序时,它会编译字节码 DEX 并将其转换为机器代码,通常称为 JIT(Just In Time)。ART 没有在每次应用程序启动时运行编译过程,而是通过在安装步骤中转换字节码 DEX 并将其转换为机器代码来改变这种方式。安装应用程序时这可能需要很长时间,但是当您启动应用程序时它会足够快。