Daily#14. Understanding JVM, Dalvik, and ART: The Engines Behind Java and Android Applications

Introduction

Developing applications in Java or for the Android platform requires an understanding of the runtime environments where these applications run. A runtime environment refers to the state of the system when a program is executed. It includes the settings, libraries, and other supporting infrastructure that the system provides to the program. Understanding runtime environments is crucial as they dictate how a program will behave when executed. Java Virtual Machine (JVM) is the original runtime environment for Java, but is not used on Android. Dalvik and ART, on the other hand, are newer runtime environments that were specifically designed for Android. This post will guide you through the functionality of JVM, Dalvik, and ART.

What is JVM?

The Java Virtual Machine (JVM) is an abstract computing machine that enables a computer to run a Java program. Here are the key characteristics of JVM:

https://en.wikipedia.org/wiki/Java_virtual_machine






  • Platform Independence: Java's "write once, run anywhere" principle is enabled by JVM's ability to interpret Java bytecode into the machine code of the host device.
  • Memory Management: JVM handles object allocation and de-allocation in memory through a process called garbage collection. Garbage collection is an automatic memory management scheme that reclaims the heap space occupied by objects that are no longer in use by the program.
  • Execution of Bytecode: JVM interprets and executes the compiled Java bytecode line by line, translating it into the machine code that the host device can understand.

What is Dalvik?

Dalvik is a virtual machine designed specifically for Android and was the default Android runtime until Android 5.0 (Lollipop), after which it was replaced by Android Runtime (ART). Here are the key characteristics of Dalvik:

  • Designed for Devices with Limited Resources: Dalvik was built keeping in mind the constraints of device resources like memory and battery. It does so by converting .class files, which are relatively heavy, into .dex (Dalvik Executable) format. The .dex files are much lighter and optimized for minimal memory footprint.
  • Executes .dex Files: Java code in Android applications is converted into .dex files, which are more memory efficient than traditional .class files used by JVM.
  • Register-Based: Unlike the JVM, which is stack-based, Dalvik is a register-based machine, typically leading to fewer instructions, but more complex ones.

What is ART?

https://source.android.com/docs/core/architecture


The Android Runtime (ART) is the current runtime used by Android applications, introduced as a replacement for Dalvik in Android 5.0 (Lollipop). ART is still under development, and new features and optimizations are being added all the time. Here are the key characteristics of ART:

  • Ahead-of-Time (AOT) Compilation: Unlike Dalvik, which used Just-In-Time (JIT) compilation, ART performs AOT compilation. This means that the bytecode is compiled into machine code upon app installation, leading to improved performance.
  • Garbage Collection: ART implements improved garbage collection over Dalvik, reducing pauses and overhead during application runtime.
  • Backwards Compatibility: ART can execute apps targeted for the Dalvik runtime by translating the Dalvik bytecode. This ensures older applications can still run on newer versions of Android, preserving the vast library of Android apps across updates.

JVM vs. Dalvik vs. ART: Key Differences

Here's a table that shows the key differences between JVM, Dalvik, and ART:

Feature JVM Dalvik ART
Execution Runs .class files Runs .dex files Compiles .dex files
Garbage Collection Generational GC Concurrent GC Improved GC
Performance Optimization Just-In-Time (JIT) Initially interpreted, later included JIT Ahead-Of-Time (AOT) compilation
Application Isolation Each app runs on separate JVM Multiple apps run concurrently within separate Dalvik instances Each app runs on separate ART instance


All three—JVM, Dalvik, and ART—have been designed with very different goals and optimizations in mind. Understanding the workings of these VMs can provide valuable insights into optimizing your Java and Android application's performance. I hope you found this comparison informative! Feel free to leave comments or ask questions below. Stay tuned for more informative posts!

Comments

Popular posts from this blog

Daily#6. Understanding and Using NetworkCallback in Android

Learning Journey#5. From Foundation to Future: Cloud Computing as a Career Pathway