DVM (Dalvik virtual machine) uses just-in-time (JIT) compilation, compiling code at runtime, while ART (Android runtime) uses ahead-of-time (AOT) compilation, compiling code during installation for better performance and faster app startup.
Key takeaways:
Dalvik was Android’s default runtime before version 5.0, using a just-in-time (JIT) compiler for on-demand code compilation.
ART (Android runtime) replaced Dalvik as the default runtime from Android 5.0 onwards, using ahead-of-time (AOT) compilation for precompiling code during installation.
Dalvik’s JIT approach was memory-efficient but slower due to runtime compilation, while ART’s AOT approach improves app startup and performance.
Dalvik virtual machine (DVM) uses optimized
.odex
files to speed up app execution.ART generates
.oat
files that store precompiled machine code, resulting in faster app performance and reduced runtime lag.
A solid grasp of runtime environments is pivotal in Android development. Dalvik and ART stand out as two prominent runtimes, crucial in converting high-level language code into machine-level code.
Let’s discuss the differences between Dalvik and ART, highlighting what makes each unique and beneficial.
Before Android version 4.4 KitKat, the default runtime was Dalvik, which utilized a just-in-time (JIT) compiler. This compiler dynamically compiles only the necessary parts of the code when needed. This approach helped save memory space, making it ideal for devices with limited storage capacity. However, this on-demand compilation process could sometimes slow down performance since it happened during actual use. Despite this drawback, it remained a preferred option for devices with low storage. In contrast, Dalvik’s method of compiling after installation made it slower compared to JIT.
In Android development, we turn Java and Kotlin code into bytecode files. These files are then optimized to run smoothly on Android devices. The final step involves creating .odex
files, which make apps faster and more efficient on Android’s Dalvik virtual machine (DVM). Let’s discuss each step in detail.
Java or Kotlin code compilation:
Conversion to Dalvik bytecode:
The Java bytecode .class
files are then passed to the dx
tool (Dex compiler) provided by the Android SDK.
The dx
tool converts the Java bytecode into Dalvik bytecode .dex
files, which are optimized for execution on Android’s Dalvik virtual machine (DVM).
Optimization of Dalvik bytecode:
The .dex
files generated by dx
are then optimized by dexopt
.
dexopt
generates optimized Dalvik bytecode files, typically with a .odex
(optimized Dalvik executable) extension.
These optimized .odex
files contain pre-compiled bytecode, improving the application’s performance by reducing the need for on-the-fly optimization by the DVM.
These optimized files are stored in the /data/dalvik-cache
directory on the device.
Handling of .odex
files:
The .odex
files are used by the Dalvik VM when executing the application.
They help speed up the application’s startup time and improve overall performance by reducing the overhead of bytecode optimization during runtime.
ART, or Android runtime, marks a shift in runtime technology. Initially introduced experimentally in KitKat and later adopted as the default runtime in Android 5.0 Lollipop, ART utilizes an ahead-of-time (AOT) compiler. Unlike JIT, which compiles code on-the-fly during runtime, ART precompiles the entire code during installation. This proactive approach drastically enhances app startup times and eradicates the lag often encountered while running apps on the device. Therefore, rather than dealing with relatively concise Java code, we contend with larger bytecode or machine code.
The first two steps (Java or Kotlin code compilation and conversion to Dalvik bytecode) are the same.
Dex code processing:
Dex code is passed to dex2oat
for optimization and compilation.
In the Android runtime (ART) environment, the integration of dex2oat
is pivotal for optimizing and compiling .dex
files into .oat
files containing machine code in the ELF
format. This process is initiated during app installation, where ART utilizes the on-device dex2oat
tool to compile apps.
Generation of .oat
files:
dex2oat
, found within Android devices, convert .dex
files to compiled app executables, enhancing performance by translating bytecode to native machine code. OAT files, standing for “Optimized Android Translated,” store this precompiled code, improving app loading and runtime performance in ART.
Utilization by ART:
These .oat
files, also known as native code, are utilized by the Android runtime (ART) to improve app loading times and runtime performance.
The key difference lies in how Dalvik compiles on the go, whereas ART compiles everything upfront during installation, fundamentally altering Android’s app execution process. Now, let’s explore the comparison across various parameters.
The latest Android operating system versions typically support both runtime environments, Dalvik and ART. While Dalvik may still be available for compatibility reasons, newer Android devices and applications primarily leverage ART for its performance benefits and optimization capabilities. Users generally do not need to manually select a runtime, as ART is automatically used by default on compatible devices.
A quick quiz to test your understanding of ART and Dalvik runtimes in Android.
What is the primary difference between Dalvik and ART in Android?
Dalvik compiles code ahead-of-time; ART compiles during runtime.
Dalvik uses JIT compilation; ART uses AOT compilation.
Dalvik and ART function the same way.
ART is slower than Dalvik.
In conclusion, while both Dalvik and ART have their advantages and trade-offs, ART’s ahead-of-time (AOT) compilation approach generally offers better performance and efficiency compared to Dalvik’s just-in-time (JIT) compilation. With AOT, ART can optimize code during installation, resulting in smoother app startup times and reduced memory consumption. Therefore, for modern Android devices and performance-critical applications, ART is often considered the better choice.
Haven’t found what you were looking for? Contact Us
Free Resources