Dalvik Virtual Machine

Dalvik Virtual Machine | DVM:-

The Dalvik Virtual Machine (DVM) is an android virtual machine optimized for mobile devices. It optimizes the virtual machine for memory, battery life and performance.
Dalvik virtual machine has few advantages over JAVA virtual machine such as:

  • Dalvik uses its own 16 bit instruction set than java 8 bit stack instructions, which reduce the dalvik instruction count and raised its interpreter speed.
  • Dalvik use less space, which means an uncompressed .dex file is smaller in size(few bytes) than compressed java archive file(.jar file)

In the Above Diagram --

  • The javac tool compiles the java source file into the class file.
  • The dx tool takes all the class files of your application and generates a single .dex file. It is a platform-specific tool.
  • The Android Assets Packaging Tool (aapt) handles the packaging process.

Role of Dalvik Virtual Machine:--
The role of dalvik virtual machine is that, In java we write and compile java program using java compiler and run
that bytecode on the java virtual machine. On the other side, In android we still write and compile java source file(bytecode)
on java compiler, but at that point we recompile it once again using dalvik compiler to dalvik bytecode(dx tool converts java .class file
into .dex format) and this dalvik bytecode is then executed on the dalvik virtual machine.

Note: Dalvik team have added Just In Time (JIT) compiler to the Dalvik Virtual Machine.
The JIT is a software component which takes application code, analyzes it, and actively translates
it into a form that runs faster, doing so while the application continues to run.

How Android code execution works?
In Android Java classes converted into DEX bytecode. The DEX bytecode format is translated to native machine code via either ART or the Dalvik runtimes. Here DEX bytecode is independent of device architecture.
Dalvik is a JIT (Just in time) compilation based engine. There were drawbacks to use Dalvik hence from Android 4.4 (kitkat) ART was introduced as a runtime and from Android 5.0 (Lollipop) it has completely replaced Dalvik. Android 7.0 adds a just-in-time (JIT) compiler with code profiling to Android runtime (ART) that constantly improves the performance of Android apps as they run.
Below are the code snippet explaining the difference between Dalvik Virtual Machine and Java Virtual Machine.
Why Java Virtual Machine is not used in android :-
There is a key aspect of replacing the Java virtual machine with the Dalvik virtual machine is its licensing. The java language, java tools and java libraries are free but the java virtual machine(which is a Stack Machines) is not. Nowadays there are other open source alternatives to Oracle Java virtual machine such as OpenJDK and Apache Harmony projects. Android provides a full featured platform by developing a truly open source and license friendly virtual machine and it also encouraged the developers and mobile companies to adopt for a variety of devices without having to worry about the license.

Post a Comment