From Android Wiki

Revision as of 08:40, 31 January 2013 by Ldo (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, searcha

Dalvik is the name of the “virtual machine” used for running Java code on Android. Traditionally, Java has been compiled, not to machine code as with languages like C or C++, but to a machine-independent, but low-level byte code. This allows the binaries to run on CPUs with different instruction sets (just so long as a byte-code interpreter is available for that CPU), with a speed somewhere in-between that of machine code and direct interpretation of the source code—hopefully closer to the former than the latter.

When Sun created Java, it defined a standard JVM (Java Virtual Machine) and byte-code. The open-sourced implementation of this interpreter, along with the Java compiler, was derived from “Java 2 Standard Edition” (J2SE), which was the Java product designed to run on regular PC-class computers; in particular, this JVM is not suited to low-power applications, such as running on a smartphone.

Separately, Sun/Oracle licenses “Java 2 Micro Edition” (J2ME), which is designed for low-power applications, and is not open-source. However, all apps on the device run within the same VM instance, which makes it unsuitable for implementing a sandbox-style security model like Android offers. Also it doesn’t apparently offer low-level access to the hardware (sensors? camera?).

For these reasons, Google decided to develop its own VM, called Dalvik. As further bonuses, the Dalvik byte-code is very compact (around half the size of JVM code, even without compression), and it claims better performance on modern CPUs because it is register-based rather than stack-based.

The Android SDK still depends on Sun/Oracle’s JDK for compiling your Java code. (The open-source OpenJDK also seems to work fine for building apps, though apparently not for building Android itself.) However, the SDK toolchain includes the dx tool which is run as an extra step to convert multiple standard JVM bytecode .class files into Dalvik bytecode in a single .dex file as part of the process of building Java code for Android. And when an app is launched on Android, the .dex code is mmap’d into memory, so that it can be paged in on demand instead of having to be read in in its entirety up front; this speeds up launching (and is also the reason why zipalign is such an important part of building an Android app).

See Also

Videos from the Android Builders’ Summit 2011, in particular Karim Yaghmour’s delving into the internals of Android, and Aleksander Gargenta’s “A Walk Through The Android Stack” (from 48:00 onwards the latter explains the choice of Dalvik over the Sun/Oracle JVM).

Personal tools