What is Dalvik's virtual Machine? Differences to a normal Java VMs?
Dalvik is the name of Android's virtual machine. It is an interpreter-only virtual machine that executes file in the Dalvik Executable(*.dex) format, a format that is optimized for efficient storage and memory-mappable execution. The VM runs on top of a Linux 2.6 kernel, which it relies on for underlying functionality (such as threading and low level memory management).
Every Android application runs in its own process, with its own instance of the Dalvik virtual machine. Dalvik has written so that device can run multiple VMs efficiently. The Dalvik VM executes files in the Dalvik Executable (*.dex) format which is optimized for minimal memory footprint. This VM is register based, and runs classes compiled by a Java language compiler that have been transformed into a .dex format by the included "dx" tool.
Given every application runs in its own process within its own virtual machine, not only must the running of multiple VMs be efficient but creation of new VMs must be fast as well.
Difference between Dalvik VM and normal JVM
- One of the first difference noted between the Dalvik VM and Java virtual Machine(JVMs) is that Dalvik is register-based, whereas the JVMs are stack-based. The register based approach allows for greater ahead-of-time optimization, which is beneficial in constrained environments like mobile phones. A more in-depth analysis concluded that register-based VMs allow for faster execution times at the expense of programs which are larger when compiled.
- Another difference between Dalvik and JVMs is the execution environment - Dalvik is optimized to allow multiple instances of the virtual machine to run at the same time in limited memory, and each Dalvik application runs as a seperate Linux process.
The Dex File format:
In Standard Java environments, Java Source code is compiled into Java bytecode, which is stored within .class files. The .class files are read by JVM at runtime. Each class in your Java Code will result in one .class file. This means that if you have, say one .java source file that contains one public class, one static inner class, and three anonymous classes, the compilation process (javac) will output 5 .class file.
On the Android platform, Java source code is still compiled into .class files. But after .class files are generated, the "dx" tool is used to convert the .class files into a .dex, or Dalvik Executable file. Whereas a .class file contains only one .class, a .dex file contains multiple classes. It is the .dex file that is executed on the Dalvik VM.
The .dex file has been optimized for memory usage and the design is primarily driven by sharing of data. The following diagram constrats the .class file format used by the JVM with the .dex file format used by the Dalvik VM.
The .dex file format uses shared, type-specific constant pools as it's primary mechanism for conserving memory. A constant pool stores all literal constant values used within the class. This includes values such as string constants used in the code as well as field, variable, class, interface, and method names. Rather than store these values throughout the class, they are always referred to by their index in the constant pool. The constant pools in the .class and .dex files are highlighted in blue above. In the case of the .class file, each class has its own private, heterogeneous constant pool. It is the heterogeneous in that all types of constants (field, variable,class etc) are mixed together. Contrast this to the .dex file that contain many classes, all of which share the same type-specific constants pool. Duplication of constants across .dex files is eliminated in the .dex file.
By allowing for classes to share constant pools, repetition of constant values is kept to a minimum. The consquence of the minimal repetition is that there are significantly more logical pointers or references within a .dex file compared to a .class file.
So it is clear that optimization of the constant pool can result in significant memory savings. The android development team found that the .dex file format cut the size in half of some of the common system libraries and applications that ship with Android.
No comments:
Post a Comment