undefined reference
undefined reference

Java vs. Dalvik VM

Written by bcopos on July 12, 2014.

Dalvik is the process virtual machine introduced and used by Android in its operating system. The Dalvik virtual machine plays an essential role in the Android OS. In order to provide isolation, each Android application runs inside its own Dalvik VM. But wait... Android applications are written in Java, no? Yes. Java class files are converted into `.dex` files by Dalvik's dex tool. So why not just use Java VM? Well, while Dalvik is similar to Java in some ways, there are some important differences.

  • Java VM
    • stack-based architecture
    • source -> bytecode (one class per .class)
    • .class file
      • header
      • heterogeneous constant pool
      • class (*this*, super class, interfaces)
      • field
      • methods
      • attributes
  • Davlik VM
    • register-based architecture
      • PROS: no overhead of `PUSH, POP` operations, optimizations (save result for sub expressions for later use)
      • CONS: instructions are larger in size on average (need to specify addresses directly)
    • source -> bytecode -> Dx (multiple classes per .dex)
    • .dex
      • header
      • strings constant pool
      • type/class constant pool
      • field constant pool
      • method constant pool
      • class definitons
      • field list
      • method list
      • code header
      • local variables
  • Differences

  • .dex has shared constant pools split by type
    • shared: eliminates redundancy
    • split by type: quicker access
  • Zygote
    • responsible for starting VM instances for new applications
    • preloads and pre-initializes core library classes (read-only, shared across processes)
    • when an application modifies any of the core library classes, Zygote writes library classes to the child process' VM and then applies the modifications ("copy-on-write" behavior minimizes memory usage but prohibits applications from interfering)