Android dexopt 和 dex2oat 的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26254538/
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
Difference between dexopt and dex2oat?
提问by Gokul Nath KP
Google
is moving from Dalvik
to ART
(Android Runtime).
Google
正在从Dalvik
到ART
(Android 运行时)。
I was trying to understand, how it is going to improve the performance.
我试图了解它将如何提高性能。
The best explanation I found is the below image:
我找到的最好的解释是下图:
One of the main component which has changed is dexopt
to dex2oat
.
其中一个已改变的主要成分是dexopt
对dex2oat
。
Since I don't have much idea about these, can anyone explain the difference and how this is going to improve the performance?
由于我对这些不太了解,谁能解释一下差异以及这将如何提高性能?
回答by JesusFreke
dexopt does some optimizations on the dex file. It does things like replacing a virtual invoke instruction with an optimized version that includes the vtable index of the method being called, so that it doesn't have to perform a method lookup during execution.
dexopt 对 dex 文件做了一些优化。它执行诸如用包含被调用方法的 vtable 索引的优化版本替换虚拟调用指令之类的事情,这样它就不必在执行期间执行方法查找。
The result of dexopt is an odex (optimized dex) file. This is very similar to the original dex file, except that it uses some optimized opcodes, like the optimized invoke virtual instruction.
dexopt 的结果是一个 odex(优化的 dex)文件。这与原始 dex 文件非常相似,只是它使用了一些优化的操作码,例如优化的调用虚拟指令。
dex2oat takes a dex file and compiles it. The result is essentially an elf file that is then executed natively. So instead of having bytecode that is interpreted by a virtual machine, it now has native code that can be executed natively by the processor. This is called AOT (ahead-of-time) compilation.
dex2oat 获取一个 dex 文件并编译它。结果本质上是一个 elf 文件,然后在本机执行。因此,现在的字节码不再是由虚拟机解释的,而是可以由处理器在本机执行的本机代码。这称为 AOT(提前)编译。
Both tools are normally run at install time on the device.
这两种工具通常在安装时在设备上运行。
Another factor to take into account is that dalvik used a JIT (just-in-time) compiler - meaning that it was also able to compile bytecode to native code. The main difference however, is that ART compiles everything ahead of time, whereas dalvik only compiled a subset of the bytecode using heuristics to detect the code that was executed most frequently, and it compiled during execution.
另一个需要考虑的因素是 dalvik 使用了 JIT(即时)编译器——这意味着它也能够将字节码编译为本机代码。然而,主要区别在于 ART 会提前编译所有内容,而 dalvik 仅使用启发式方法编译字节码的一个子集,以检测最常执行的代码,并在执行期间进行编译。
回答by Ritesh Jha
Android Runtime (ART)is an application runtime environment used by the Android mobile operating system. ART replaces Dalvik, which is the process virtual machine originally used by Android, and performs transformation of the application's bytecode into native instructions that are later executed by the device's runtime environment.
Android Runtime (ART)是 Android 移动操作系统使用的应用程序运行时环境。ART 取代了 Dalvik,后者是 Android 最初使用的进程虚拟机,将应用程序的字节码转换为本地指令,稍后由设备的运行时环境执行。
Unlike Dalvik, which since Android 2.2 "Froyo" uses just-in-time (JIT) compilation to compile the bytecode every time an application is launched, ART introduces use of ahead-of-time (AOT) compilation by performing it upon the installation of an application. By reducing the overall amount of compilation that needs to be performed across the operation of an application, a mobile device's processor usage is reduced and battery runtime is improved. At the same time, ART brings improvements in performance, garbage collection, applications debugging and profiling.
与 Dalvik 不同,因为 Android 2.2“Froyo”使用即时 (JIT) 编译在每次应用程序启动时编译字节码,ART 引入了使用提前 (AOT) 编译,在安装时执行它的一个应用程序。通过减少在应用程序操作过程中需要执行的总编译量,移动设备的处理器使用量减少,电池运行时间得到改善。同时,ART 带来了性能、垃圾收集、应用程序调试和分析方面的改进。
To maintain backward compatibility, ART uses the same input bytecode as Dalvik, supplied through standard .dex
files as part of APK files, while the .odex
files are replaced with Executable and Linkable Format (ELF) executables. Once an application is compiled by using ART's on-device dex2oat utility, it is run solely from the compiled ELF executable; this approach eliminates various overheads involved with JIT compilation, but it requires additional time for compilation when an application is installed, and applications take up slightly larger amounts of space to store the compiled code.
为了保持向后兼容性,ART 使用与 Dalvik 相同的输入字节码,通过标准.dex
文件作为 APK 文件的一部分提供,而这些.odex
文件被替换为可执行和可链接格式 (ELF) 可执行文件。一旦使用 ART 的设备上 dex2oat 实用程序编译了应用程序,它就只能从编译的 ELF 可执行文件中运行;这种方法消除了 JIT 编译所涉及的各种开销,但是在安装应用程序时需要额外的编译时间,并且应用程序占用稍大的空间来存储编译后的代码。