Middle
What is the difference between Dalvik Virtual Machine and ART (Android Runtime)?
sobes.tech AI
Answer from AI
The main differences between Dalvik and ART are as follows:
-
Compilation:
- Dalvik: Uses Just-In-Time (JIT) compilation. Bytecode is compiled into machine code at runtime.
- ART: Uses Ahead-Of-Time (AOT) compilation by default. Bytecode is compiled into native code during the first installation or update of the app. Starting from Android 7.0 (Nougat), ART also includes JIT compilation for performance optimization during runtime when collecting information about "hot" code sections.
-
Performance:
- Dalvik: JIT compilation causes delays during execution as compilation happens "on the fly".
- ART: AOT compilation provides higher performance during runtime as most compilation is done in advance. However, installing and updating apps may take longer due to compilation.
-
Power consumption:
- Dalvik: JIT compilation may consume more energy during app operation due to constant compilation.
- ART: AOT compilation can be more energy-efficient during app operation since most compilation is completed before launch.
-
Memory and disk space usage:
- Dalvik: DVM files (Dalvik Executable) are smaller in size. Native code is generated during execution.
- ART: ART uses OAT files (Optimized Android Executable) that contain optimized native code. These files occupy more disk space but allow faster app startup.
-
Garbage Collection:
- Dalvik: Less efficient garbage collection algorithms, which could lead to "hiccups" in app performance.
- ART: Improved garbage collection with algorithms designed to reduce pauses, resulting in smoother animations and interface responsiveness.
-
Tools and debugging:
- Dalvik: Debugging and profiling could be more complex due to JIT compilation.
- ART: Enhanced support for debugging and profiling native code.
Brief comparison table:
| Characteristic | Dalvik Virtual Machine (DVM) | ART (Android Runtime) |
|---|---|---|
| Compilation type | JIT (Just-In-Time) | AOT (Ahead-Of-Time) by default, JIT since Android 7.0 |
| Performance | Lower during runtime | Higher during runtime |
| Startup time | Faster on first launch | Slower during installation/update, faster on launch |
| Power consumption | May be higher during operation | May be lower during operation |
| Disk usage | Smaller | Larger (due to OAT files) |
| Garbage collection | Less efficient | More efficient |
| Support | Deprecated (up to Android 4.4) | Current runtime environment |
ART replaced Dalvik starting from Android 5.0 (Lollipop) and is the standard runtime environment for modern Android versions. An example code for illustrating compilation differences is not applicable, as these are low-level internal OS components, not application code. We use J language, upon which Dalvik or ART operates.
// Example of a simple Android app in Java/Kotlin,
// which compiles into Dalvik/ART bytecode (.dex file).
// The app code itself looks the same regardless of the runtime environment.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.myTextView);
textView.setText("Hello, ART!"); // This code runs on Dalvik or ART
}
}