Difference between dexopt and dex2oat?
Asked Answered
T

2

50

Google is moving from Dalvik to ART(Android Runtime).

I was trying to understand, how it is going to improve the performance.

The best explanation I found is the below image:

Dalvik and ART

One of the main component which has changed is dexopt to dex2oat.

Since I don't have much idea about these, can anyone explain the difference and how this is going to improve the performance?

Torosian answered 8/10, 2014 at 10:21 Comment(2)
Could you share the source of this image?Mercerize
@Mercerize en.wikipedia.org/wiki/File:ART_view.pngHerrin
G
80

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.

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.

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.

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.

Gaur answered 8/10, 2014 at 17:40 Comment(9)
can you tell me whether this converted elf formatted OAT is the final set of instructions to run or further compilation or optimization is made? Actually I am trying to do some static analysis over the oat converted file and later map the instructions to the original runtime instruction trace. And finally can you tell me which files to look for in android source code to have a clear idea?Furmark
Yes, the oat file is directly executed. You'll want to look at the art project in AOSP.Gaur
Hi, do you have any idea where these AOT files are stored? Is it the path /data/dalvik-cache/prfiles ?Furmark
Yes, they will generally be in /data/dalvik-cacheGaur
This may be a silly question, but if dex2oat produces a native code which uses "Ahead of Time" (AOT) compilation, why isn't called dex2aot? May be a typo or it means something altogether?Livraison
Art's AOT-compiled image files are called oat files. I don't know the meaning/history of "oat", but I'm guessing it's an intentional creative misspelling of aot.Gaur
Colo, so I acn be craetive, oto! ;-)Lettering
@Livraison I found this question: What does OAT mean?Morose
Why doesn't Google replace DEX altogether? javac compiles .java to JVM bytecode. Then dx compiles JVM bytecode to DVM bytecode. Then dex2oat compiles DVM bytecode to ART bytecode/machine code. Why can't it make an ax that compiles JVM bytecode to ART?Molding
S
4

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.

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.

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.

Stokes answered 12/10, 2018 at 4:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.