Android operating system is truly an open source in nature. Technically it proves its presence in current smartphones market. As a user we use android functionality in our smartphones and continuously tried to explore new applications on this platform. But as a developer we worked hard to build android applications and ensures its complete development. Everyone knows Android Application is developed in Java language. But, At the same time there is misconception about use of java in building an application. In this article, I will talk about technical side of android operating system and the role of Dalvik Virtual Machine (DVM) in android applications.
Note: To know Interesting Facts About Java, Read here
What is a Virtual Machine?
A virtual machine is like a software implementation of a physical computer which works like real physical computer. It means a virtual machine can compile and run any program just like a physical computer does for us.
But there is a dark-side of virtual machine as it is less efficient than real physical computer. It provides unstable performance when multiple virtual machine exist on the same machine.
What is JVM (Java Virtual Machine)?
JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides runtime environment in which java bytecode can be executed.
The JVM performs following operation:
- Loads the code
- Verifies the code
- Executes the code
- Provides runtime environment
How does Java run on Android with the JVM?
It doesn’t. Don’t confuse Java the Programming language with the Java Platform.
For the Java Platform, Java Bytecode is executed by the Java Virtual Machine. The compiler converts source code written in the Java Programming Language into bytecode for the Dalvik Virtual Machine.
For Android, the compiler converts source code written in the Java Programming Language into bytecode for the Dalvik Virtual Machine.
Android does not have a JVM. The JVM and DVM work in entirely different ways. One is a stack-based system, the other is a register based system.
What is DVM (Dalvik Virtual Machine)?
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 is a name of a town in Iceland. It was originally written by Dan Bornstein, who named it after the fishing village of Dalvik in Iceland, where some of his ancestors lived.
While developing Dalvik Virtual Machine Dan Bornstein and his team realize the constraints specific to the mobile environment which is not going to change in future at least, like battery life, processing power and many more. So they optimized the dalvik virtual machine.
How DVM works?
The Dex compiler converts the class files into the .dex file that run on the Dalvik VM. Multiple class files are converted into one dex file.
Let’s see the compiling and packaging process from the source file:
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.
Why Java Virtual Machine is not used?
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.
Why cannot you run standard Java bytecode on Android?
Android uses Dalvik Virtual Machine (DVM) which requires a special bytecode. We need to convert Java class files into Dalvik Executable files using and Android tool called “dx”. In normal circumstances, developers will not be using this tool directly and build tools will care for the generation of DVM compatible files.
Can you deploy executable JAR’s on Android? Which packaging is supported by Android?
No. Android platform does not support JAR deployments. Applications are packed into Android Package (.apk) using Android Asset Packaging Tool (aapt) and then deployed on to Android platform. Google provides Android Development Tools for Eclipse that can be used to generate Android Package.
What are Dalvik Executable files?
Dalvik Executable files have .dex extension and are zipped into a single .apk file on the device.
Android programs are compiled into .dex (Dalvik Executable) files, which are in turn zipped into a single apk file on the device.
.dex files can be created by automatically translating compiled applications written in the Java programming language.
The extension for an Android package file, which typically contains all of the files related to a single Android application. The file itself is a compressed collection of an AndroidManifest.xml file, application code (.dex files), resource files, and other files. A project is compiled into a single .apk file.
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.
JVM vs DVM
Dalvik is said to be a clean-room implementation rather than a development on top of a standard Java runtime, which would mean it does not inherit copyright-based license restrictions from either the standard-edition or open-source-edition Java runtimes. Dalvik is published under the Apache 2 license.
ART and Dalvik
Android runtime (ART) is the managed runtime used by applications and some system services on Android.
ART and its predecessor Dalvik were originally created specifically for the Android project. Android Runtime (ART) as the runtime executes the Dalvik Executable format and Dex bytecode specification.
ART and Dalvik are compatible runtimes running Dex bytecode, so apps developed for Dalvik should work when running with ART. However, some techniques that work on Dalvik do not work on ART.
Here are some of the major features implemented by ART.
Ahead-of-time (AOT) compilation
Improved garbage collection
Development and debugging improvements
Support for sampling profiler
Support for more debugging features
Improved diagnostic detail in exceptions and crash reports
Why is ART better than Dalvik?
ART (Android RunTime) is the next version of Dalvik. Dalvik is the runtime, bytecode, and VM used by the Android system for running Android applications.
ART has two main features compared to Dalvik:
- Ahead-of-Time (AOT) compilation, which improves speed (particularly startup time) and reduces memory footprint (no JIT)
- Improved Garbage Collection (GC)
AOT means your apps are compiled to native code once. What is stored on your phone and run is effectively native, not bytecode. This differs from the traditional VM model, which interprets bytecode. Interpreters are slow, so VM developers added a technology called Just-in-Time (JIT) compilation, which compiles (and hopefully optimizes) your code to native code on-the-fly. Dalvik is a JIT’ing VM. The downside to JIT is that the JIT compiler runs while you are using your app, adding latency and memory pressure. The upside is that the JIT compiler can look at how you are using your code to perform profile-directed optimizations.
AOT is like JIT, but it runs once—say, at app installation time. While it lacks the ability to perform profile-directed optimizations, it is possible to perform more extensive optimizations since the compilation is less time sensitive. AOT is useful on systems, such as mobile devices, where JIT adds an unacceptable latency or memory cost to running apps. I think AOT is the right step for Android and ART looks quite impressive.
ART was first included in Android KitKat, but isn’t yet enabled by default. You can enable it via Settings > Developer options > Select runtime > Use ART.
Benefits of ART
- Reduces startup time of applications as native code is directly executed.
- Improves battery performance as power utilized to interprete byte codes line by line is saved.
- As JIT is not in the picture on the fly compilation and native code storage. This significantly reduces memory footprint (less RAM is required for application to run).
Drawbacks of ART
- As dex bytecodes are converted to native machine code on installation itself, installation takes more time.
- As the native machine code generated on installation is stored in internal storage, more internal storage is required.