Tuesday, September 4, 2012

Java virtual machine(JVM) vs. Dalvik virtual machine(DVM)

Although Android applications are implemented in Java, but each application in android is run in Dalvik Virtual Machine(DVM) with unique UNIX uid. DVM substantially differ from Java Virtual Machine(JVM) with following aspects.
  • Following diagram shows the compilation of java source code both in JVM and DVM. Java byte code is directly run on JVM in java devices but in android, java byte code(.class file) recompile by dex compiler and dalvik byte code(.dex file) is generated. That dalvik executable is run on dalvik virtual machine in android devices.


  • JVM compiles the java source code to multiple .class file, one per class while DVM compiles the java byte code to single .dex file containing all classes.
  • The DVM is registers based while JVM is stack based. Dalvik byte code assigns local variables to any of 216 available registers. In java, opcodes access elements in program stack for manipulation while in DVM, opcodes directly manipulate registers.
  • Also the bytecode instruction set for java and dalvik are different. Dalvik has 218 opcodes while java has 200.Dalvik instruction set are longer than java instruction set as it often includes the source and destination register. So as result dalvik application has fewer instructions than java but have larger code size.
  • Java application replicate elements in constant pools within the multiple .class files. The dx compiler eliminate much of this replication by using single pool that all classes simultaneously refer. Also it eliminate some constants by inlining them in bytecode.
  • The dalvik bytecode does not specify the null type, instead uses zero value constant.

1 comment: