ART是一个安装程序?或者一个操作系统?或者是一个虚拟机?

先看一下基维百科的描述:

Android Runtime(缩写为ART),是一种在Android操作系统上的运行环境,在Android 5.0及后续Android版本中作为正式的运行时库取代了以往的Dalvik虚拟机。ART能够把应用程序的字节码转换为机器码,是Android所使用的一种新的虚拟机。它与Dalvik的主要不同在于:Dalvik采用的是JIT技术,而ART采用Ahead-of-time(AOT)技术。ART同时也改善了性能、垃圾回收(Garbage Collection)、应用程序除错以及性能分析。

在应用程序启动时,JIT通过进行连续的性能分析来优化程序代码的执行,在程序运行的过程中,Dalvik虚拟机在不断的进行将字节码编译成机器码的工作。

与Dalvik虚拟机不同的是,ART引入了AOT这种预编译技术,

在应用程序安装的过程中,ART就已经将所有的字节码重新编译成了机器码。应用程序运行过程中无需进行实时的编译工作,只需要进行直接调用。

因此,ART极大的提高了应用程序的运行效率,同时也减少了手机的电量消耗,提高了移动设备的续航能力,在垃圾回收等机制上也有了较大的提升。

为了保证向下兼容,ART使用了相同的Dalvik字节码文件(dex),即在应用程序目录下保留了dex文件供旧程序调用然而.odex文件则替换成了可执行与可链接格式(ELF)可执行文件。一旦一个程序被ART的dex2oat命令编译,那么这个程序将会指通过ELF可执行文件来运行。

因此,相对于Dalvik虚拟机模式,ART模式下Android应用程序的安装需要消耗更多的时间,同时也会占用更大的储存空间(指内部储存,用于储存编译后的代码),但节省了很多Dalvik虚拟机用于实时编译的时间。



While OAT file is an ELF file, it is not your usual executable ELF with everything in it including the entrypoint. It actually has a small number of symbols that are beginning and end of a binary blob which in turn contains the executable code of compiled methods. Now which methods should be called or when is all determined by runtime.

How it all works - a class is loaded. There is a C++ object that represents the Java object that is that class. Then an instance of that class is created. That's another C++ object. Than a method of that object is called. You find the object's class and find the method entrypoint (which is the compiled code). That code calls some other method - for that you will return back to runtime code, find another object and its class and call the new method again. There are no direct calls between methods (that I know of).

All this linking and resolution is actually done at each application start. Bootclasspath stuff is initialized once and then the process is forked so the children don't have to re-init bootclasspath (it's a bit more complex than that because of boot.art actually).

So the ART is still a JVM and OAT files are just some intermediate representation of some pre-compiled code. The fact that they are ELF files doesn't help much - could be something else.

P.S. Actually OAT file also contains all the DEX files which were used to create it. It is needed to construct the classes and methods. It is also possible to run application in interpreter mode when it actually executes DEX instructions directly.


上文中有人认为ART也是一种JVM,那么JVM是什么:
Java虚拟机有自己完善的硬体架构,如处理器、堆栈、寄存器等,还具有相应的指令系统。JVM屏蔽了与具体操作系统平台相关的信息,使得Java程序只需生成在Java虚拟机上运行的目标代码(字节码),就可以在多种平台上不加修改地运行。通过对中央处理器(CPU)所执行的软件实作,实现能执行编译过的Java程序码(Applet与应用程序)。


上图我们可以看出JVM需要字节码(bytecode),如:.class、.jar文件。

而通过编译、打包android应用的过程,我们知道.java文件编译后得到.class文件,.class文件通过dex工具编译成.dex文件,然后将资源文件和.dex文件打包成.apk文件。

对比JVM和Dalvik VM,可以发现:

JVM运行.class文件;DVM运行.dex文件。

而ART也要兼容DVM的.dex规范。所以ART与DVM都是运行字节码(bytecode)的虚拟机。


这时候我们再回过头看Android runtime的解释:

Android Runtime (ART) is an application runtime environment used by the Android operating system. ART replaces Dalvik, which is the process virtual machine originally used by Android, and transforms the application's bytecode into native instructions that are later executed by the device's runtime environment.[1]

ART是一个供Android操作系统使用的应用程序运行时环境。ART取代了Dalvik,将应用程序的字节码转换成机器码(native instructions / machine language),以供设配的运行时环境调用执行。

由此可以看出ART与Dalvik作用都是运行字节码文件,将其转换成机器码,并交付给设配的运行时环境来调用执行。

注意:这里提到两个运行时(runtime)一个是Android runtime(ART);另一个是设备的runtime。关于runtime,IOS开发也有object-c runtime,谷歌浏览器开发有chrome rumtime.


那么ART为什么比Dalvik快呢?

编译方式方面:ART采用Ahead of Time(AOT)方式编译;Dalvik采用Just In Time(JIT)方式编译。ART的提前编译,只编译一次的策略更有优势。其次避免了实时编译的弊端,还得到了省电的效果。

另外,无论是AOT还是JIT,目的都是通过编译,将整个应用程序编译成机器码。

Unlike Dalvik, ART introduces the use of ahead-of-time (AOT) compilation by compiling entire applications into native machine code upon their installation.


那么通过上面的描述,是不是ART就是使用AOT策略将字节码文件编译成机器码?那我们再看一下AOT是怎么执行的。


Ahead-of-time (AOT) compilation is the act of compiling a high-level programming language such as C or C++, or an intermediate language such as Java bytecode or .NET Common Intermediate Language (CIL) code, into a native (system-dependent) machine code with the intention of executing the resulting binary file natively.

AOT编译是一种将高级程序语言,如C或C++,或中间语言如java bytecode或.net CIL code,编译成一种本地机器码的行为。


just-in-time (JIT) compilation, also known as dynamic translation, is compilation done during execution of a program – at run time – rather than prior to execution.

JIT compilation is a combination of the two traditional approaches to translation to machine code – ahead-of-time compilation (AOT), and interpretation。

JIT编译,也叫动态转换,是一种在程序执行期间(即,运行时)完成编译的行为,而不是在执行之前。

JIT编译是两种传统机器码翻译方式的组合--AOT和interpretation.


在应用程序安卓过程中,

ART的AOT通过dex2oat可执行文件将字节码文件dex转成机器码码文件oat。(oat是一种elf文件,elf是一种机器码文件。)

native code (Windows: PE, PE32+, OS X/iOS: Mach-O, Linux/Android/etc: ELF);

The runtime (CLR for .net or JVM for Java);

而Dalvik只是通过dexopt可执行文件将字节码文件dex优化成了更快的字节码文件odex。并没有执行JIT编译,也就没有将字节码转换成机器码。


接下来,再看一下runtime:

runtime system:一种设计用来执行计算机程序的软件。

A runtime system, also called run-time system, primarily implements portions of an execution model. This is in contrast to the runtime lifecycle phase of a program, during which the runtime system is in operation. Most languages have some form of runtime system, which implements control over the order in which work that was specified in terms of the language gets performed. Over the years, the meaning of the term 'runtime system' has been expanded to include nearly any behaviors that are dynamically determined during execution.

运行时系统,也叫run-time系统,主要实现了部分的执行模型。运行时系统运行期间,就相当于一个程序的运行时生命周期阶段。大部分语言都有某种形式的运行时系统,用于实现控制既定程序的执行顺序。多年来,运行时系统术语的意义也已拓展为包含在执行期间几乎所有动态确定的行为。

runtime library:一种实现了构造程序语言的方法的程序库。


以java的运行时系统(java runtime system / java runtime environment)为例,包含 Java Virtual Machine (Java interpreter)、core class library and supporting files、all of which are required to run a Java program.


To put in simplest terms, runtime comprises of software instructions that execute when your program is running, even if they’re not essentially are a part of the code of that piece of software in particular. These instructions basically translate the software’s own code into the code that the computer is capable of running. Therefore, all computer languages require some sort of runtime environment that can properly execute the code written in that language.


Android makes use of a virtual machine as its runtime environment in order to run the APK files that constitute an Android application. The advantage of using a virtual machine is twofold – firstly, the app code is isolated from the core operating system, ensuring that should something go wrong, it’s contained in an isolated environment and does not effect the primary OS. And secondly, it allows for cross-compatibility, meaning even if an app is compiled on another platform (such as a PC, as is usually the case with developing mobile apps) , they can still be executed on the mobile platform using the virtual machine.

For Android, the virtual machine-based runtime environemnt in use so far is known as the Dalvik Virtual Machine, which, I’m sure anyone who’s ever digged into the details of the OS, is more than familiar with.




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值