Differences between Dalvik and ART
Dalvik is a discontinued process virtual machine (VM) that was used by Android to execute applications written in Java. It was the original runtime for Android, designed to run on devices with limited memory and processing power. Dalvik was optimized to allow multiple instances to run on a single device, each with its own virtual machine. It used a Just-In-Time (JIT) compilation model, where code was compiled into native machine code only when needed during execution, which helped in conserving memory and resources.
ART (Android Runtime):
ART, which stands for Android Runtime, is the successor to Dalvik and is the current runtime environment for Android applications. It was introduced in Android 4.4 KitKat as an experimental feature and became the default runtime starting from Android 5.0 Lollipop. Unlike Dalvik, ART uses Ahead-Of-Time (AOT) compilation, where the entire application code is compiled into native machine code during installation, rather than at runtime. This approach improves performance and reduces battery consumption.
Table of Contents
Differences Between Dalvik and ART
1. Compilation Method:
Dalvik: Uses Just-In-Time (JIT) compilation. Code is compiled into native machine code on-the-fly during execution.
ART: Uses Ahead-Of-Time (AOT) compilation. Code is compiled into native machine code during the app’s installation.
2. Performance:
Dalvik: Slower app execution because of JIT compilation. Each time an app runs, the code is compiled.
ART: Faster app execution due to AOT compilation. The code is pre-compiled, leading to quicker startup times.
3. Memory Usage:
Dalvik: Lower memory usage during installation, but higher memory usage at runtime due to JIT compilation.
ART: Higher memory usage during installation because of AOT compilation, but lower memory usage at runtime.
4. Battery Consumption:
Dalvik: Higher battery consumption due to the continuous JIT compilation during app execution.
ART: Lower battery consumption as the code is already compiled into machine code.
5. App Installation Time:
Dalvik: Faster installation because JIT compilation is done during execution.
ART: Slower installation because AOT compilation happens during installation.
Java Example: Code Behavior Under Dalvik and ART
While we cannot directly showcase how Dalvik and ART compile Java code, we can illustrate a simple example that would behave differently under these two runtimes, particularly in terms of performance.
Example of Code:
```java
public class PerformanceTest {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
int sum = 0;
for (int i = 0; i < 1000000; i++) {
sum += i;
}
long endTime = System.currentTimeMillis();
System.out.println("Execution Time: " + (endTime - startTime) + " ms");
}
}
```
Explanation of the Code:
This simple Java program calculates the sum of the first 1,000,000 integers.
The execution time is measured in milliseconds to see how long it takes to complete the loop.
Behavior Under Dalvik and ART:
- Dalvik: The first time this code runs, it will take longer because Dalvik will compile the bytecode into native machine code as it executes the loop.
- ART: The code will run faster because ART would have already compiled the code into native machine code during the app’s installation, so no additional compilation is needed during runtime.
This example is a simple representation of how performance can differ between Dalvik and ART.