Dalvik Virtual Machine
Dalvik Virtual Machine
P.N. Anantharaman
Director Engineering, Adobe Systems India
26 March 2011
The application written in Java source code gets compiled to Java class.
This in turn is converted to DVM byte code by a tool called dx. The DVM
byte code file has a .dex extension.
The DVM loads the application classes from the dex file, executes the
application similar to the way JVM does for a Java class file (albeit several
key differences)
Why DVM? (particularly when JVM already exists and the Android
application programming model is Java based)
What is DVM?
Byte code description
Dex file format
DVM run time design (Interpreter and JIT compiler)
Programming the DVM using byte code assembly (using
assemblers/disassemblers like Smali, Baksmali)
Goals
Run on a slow CPU
Limited RAM
OS without swap space
Battery Powered
RAM
ODROID-7: 512 MB
HTC Desire: 576 MB
iPhone: 512 MB
Available RAM gets reduced after the low level system start up and high
level services
Multiple independent mutually suspicious processes
Separate address spaces and separate memory
Large system library
How does the dex file (produced by dx tool) differs from class file
(produced by javac)?
In what ways DVM execution environment optimize memory as compared
to JVM based execution?
Does the DVM enforce any security policies on the application that it
executes?
What is a JIT compiler for DVM? What are the advantages and
disadvantages of a JIT compiler?
Consider an example:
public int method( int i1,int i2 ) {
int i3 = i1*i2;
return i3*2;
}
Stack based machines use the stack to hold the operands, perform the
operation where the result is also stored on the stack
iload_1
iload_2
imul
istore_3
iload_3
iconst_2
imul
ireturn
Stack based VMs have a simple architecture and provide ease of writing of
compiler back end. The JVM is stack based.
One stack level can hold any type (char to float)
Long and double need 2 consecutive stack levels
Register-based architecture requires and average of 47% less executed VM
instructions than the stack based [architecture]. On the other hand the
register code is 25% larger than the corresponding stack code but this
increased cost of fetching more VM instructions due to larger code size
involves only 1.07% extra real machine loads per VM instruction which is
negligible. The overall performance of the register-based VM is that it takes
on average 32.3% less time to execute standard benchmarks
Security Engineering Research Group, Institute of Management Sciences. “Analysis of
Dalvik Virtual Machine and Class Path Library 2009”
Each invoked method has its own set of registers. Hence the registers
can be treated like local variables
Invoked methods don’t affect the registers of invoking methods
FieldDescriptor : FieldType
ComponentType : FieldType
FieldType : BaseType | ObjectType | ArrayType
BaseType
ObjectType : L<classname>;
ArrayType : [ ComponentType
ParameterDescriptor : FieldType
MethodDescriptor : (ParameterDescriptor*) ReturnDescriptor
ReturnDescriptor : FieldType | V
Base types
I – int
J – long
Z – boolean
D – double
F – float
S – short
C – char
B – Byte
The VM is register based and frames are fixed in size upon creation.
Each frame consists of a fixed number of registers as well as any adjunct
data required to execute the method
The N arguments to a method land in the last N registers of methods
invocation frame
Registers are 32 bit wide – adjacent registers can be combined for
double precision
(Object) null == (int) 0
The storage unit in the instruction stream is a 16 bit unsigned quantity.
Some bits in some instructions are ignored or must be zero
Destination – then – source ordering for arguments
Suffix “wide” refers to 64 bit quantities
Static methods: in the DVM byte code implementing the method, “this” is
not passed on as the first argument
Direct: these are method invocation where the derived classes can’t
override the parent’s method. Hence these methods are invoked directly
without involving the class’s vtable
Virtual: Derived classes can override the methods of their parent classes
from where they were derived. These are invoked using a vtable
associated with the class
Reference: http://www.netmite.com/android/mydroid/dalvik/docs/dalvik-
bytecode.html