The Dalvik Virtual Machine
The DalvikVM is register-based, as opposed to stack-based. Although Dalvik is
said to be Java-based it is not Java insofar as Google does not use the Java logos and
the Android application model has no relationship with JSRs (Java Specifi cation
Requirements). To the Android application developer, the DalvikVM might look
and feel like Java but it isn’t. The overall development process looks like this:
1. Developer codes in what syntactically looks like Java.
2. Source code is compiled into .class fi les (also Java-like).
3. The resulting class fi les are translated into Dalvik bytecode.
4. All class fi les are combined into a single Dalvik executable (DEX) fi le.
5. Bytecode is loaded and interpreted by the DalvikVM.
As a register-based virtual machine, Dalvik has about 64,000 virtual regis-
ters. However, it is most common for only the fi rst 16, or rarely 256, to be used.
These registers are simply designated memory locations in the VM’s memory
that simulate the register functionality of microprocessors. Just like an actual
microprocessor, the DalvikVM uses these registers to keep state and generally
keep track of things while it executes bytecode.
The DalvikVM is specifi cally designed for the constraints imposed by an
embedded system, such as low memory and processor speeds. Therefore, the
DalvikVM is designed with speed and effi ciency in mind. Virtual machines,
after all, are an abstraction of the underlying register machine of the CPU. This
inherently means loss of effi ciency, which is why Google sought to minimize
these effects.
To make the most within these constraints, DEX fi les are optimized before
being interpreted by the virtual machine. For DEX fi les launched from within
an Android app, this generally happens only once when the application is fi rst
launched. The output of this optimization process is an Optimized DEX file (ODEX). It should be noted that ODEX fi les are not portable across different
revisions of the DalvikVM or between devices.
Similar to the Java VM, the DalvikVM interfaces with lower-level native code
using Java Native Interface (JNI). This bit of functionality allows both calling from
Dalvik code into native code and vice versa. More detailed information about the
DalvikVM, the DEX fi le format, and JNI on Android is available in the offi cial
Dalvik documentation at http://milk.com/kodebase/dalvik-docs-mirror/docs/.