The difference between JIT and ART, ORT

JIT与Dalvik

JIT is the abbreviation of "Just In Time Compiler", which is "just-in-time compilation technology" and is related to the Dalvik virtual machine.

How to understand this sentence? This starts with some features of Android.

JIT was proposed in version 2.2 to improve the running speed of Android and survived to version 4.4, because in the ROM after 4.4, there is no Dalvik virtual machine.

We use Java to develop android. When compiling and packaging the APK file, it will go through the following process

  • The Java compiler compiles all Java files in the application into class files
  • The dx tool converts the class file output by the application compilation into Dalvik bytecode, that is, the dex file

After that, it becomes an APK file after signature, alignment and other operations.

The Dalvik virtual machine can be regarded as a Java VM. It is responsible for interpreting the dex file as machine code. If we do not process it, every time we execute the code, we need Dalvik to translate the dex code into microprocessor instructions, and then hand it over to the system for processing. , which is inefficient.

In order to solve this problem, Google added the JIT compiler in version 2.2. When the App is running, whenever a new class is encountered, the JIT compiler will compile the class, and the compiled code will be optimized to be equivalent The simplified native instruction code (that is, native code), so that the next time the same logic is executed, the speed will be faster.

Of course, using JIT does not necessarily speed up execution. If most of the code is executed very few times, the time spent compiling is not necessarily less than the time spent executing dex. Of course Google also knows this, so JIT does not compile all dex code, but only compiles the dex that is executed more frequently as local machine code.

One thing to note is that the translation of dex bytecode into local machine code occurs during the running process of the application, and every time the application is re-run, the translation work must be redone, so this work does not Not once and for all, every time the app is reopened, JIT compilation is required.

In addition, the Dalvik virtual machine has lived from the birth of Android to version 4.4, and JIT did not exist when Android was first released, and was added to Dalvik after 2.2.

ART and AOT

AOT是"Ahead Of Time"的缩写,指的就是ART(Anroid RunTime)这种运行方式。

前面介绍过,JIT是运行时编译,这样可以对执行次数频繁的dex代码进行编译和优化,减少以后使用时的翻译时间,虽然可以加快Dalvik运行速度,但是还是有弊病,那就是将dex翻译为本地机器码也要占用时间,所以Google在4.4之后推出了ART,用来替换Dalvik。

在4.4版本上,两种运行时环境共存,可以相互切换,但是在5.0+,Dalvik虚拟机则被彻底的丢弃,全部采用ART。

ART的策略与Dalvik不同,在ART 环境中,应用在第一次安装的时候,字节码就会预先编译成机器码,使其成为真正的本地应用。之后打开App的时候,不需要额外的翻译工作,直接使用本地机器码运行,因此运行速度提高。

当然ART与Dalvik相比,还是有缺点的。

  • ART需要应用程序在安装时,就把程序代码转换成机器语言,所以这会消耗掉更多的存储空间,但消耗掉空间的增幅通常不会超过应用代码包大小的20%
  • 由于有了一个转码的过程,所以应用安装时间难免会延长

但是这些与更流畅的Android体验相比而言,不值一提。

总结

通过前面背景知识的介绍,我终于可以更简单的介绍这四个名词之间的关系了:

  • JIT代表运行时编译策略,也可以理解成一种运行时编译器,是为了加快Dalvik虚拟机解释dex速度提出的一种技术方案,来缓存频繁使用的本地机器码
  • ART和Dalvik都算是一种Android运行时环境,或者叫做虚拟机,用来解释dex类型文件。但是ART是安装时解释,Dalvik是运行时解释
  • AOT可以理解为一种编译策略,即运行前编译,ART虚拟机的主要特征就是AOT

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326679843&siteId=291194637