In the previous article on Android Internals, we learnt how Android OS Starts an application. We mentioned many things such as Bootloader, Kernel, init process, Zygote, DVM and System server.
In this post, we’ll take a look at the runtime environment in Android and how it has changed over the years. More specifically, we’ll be having a comparing ART vs DVM in Android on multiple factors such as installation time, runtime performance and other optimizations such as app size.
But to understand this, let’s first start with some basics such as what is a Virtual Machine? Stack based vs Register Based architecture of VM and comparing JVM vs DVM.
Virtual Machine is an abstraction over the native machine backed by the resources of the native machine. It’s job is to convert language specific code to a format compatible to run on the Virtual Machine. Virtual Machine enables the same code to be run on multiple platform independent of the underlying hardware.
For example the JVM runs java bytecode and produces same output on multiple platforms. Bytecode produced in windows machine will be able to run on a JVM running in UNIX machine.
Note: JVM is not platform independent. It is written in C/C++ and is platform dependent. You’ll always need something to communicate with the underlying architecture.
A virtual machine should be able to carry out all the operations of a physical CPU. These include:
Compilation of source code into VM specific code.Data Structure to contain operands and instructions.Instruction PointerA Virtual CPU
We’ll be looking at the two ways of implementing a Virtual Machine:
Stack is a LIFO data structure, which means what comes in last is computed first. If you’ve taken a class on Computer Architecture, you’d be familiar with how arithmetic operations work in case of Stack Based Architecture.
Let’s take an example. Consider the stack given below:
Now, in order to compute the sum of the first two operands, the chain of command would be:
Here are the actual CPU instructions:
Note here that for addition of two integers, we’ve generated 3 lines of code.
Some Advantages of Stack based architecture:
Some Disadvantages of Stack based architecture:
Register is a small place inside the processor which can hold various types of data such as operands, instruction, memory location etc.
The length of instruction a register can store, depends upon the architecture of the machine. For example: 64 bit machine has registers which can hold 64 bit instructions.
Here is the format of instruction in a MIPS32 register based architecture:
Now, if we were to perform the same addition operation as in Stack Based register, it would look something like this:
What this instruction essentially means is that, add R2 and R3 and store the result in R1. Notice how we complete the operation with a single instruction. This is the main advantage of Register based architecture over Stack based (which took 3).
On an average, register based architecture has to run 47% less instructions than stack based. But the register code is 25% larger than stack code.
This knowledge of Stack based architecture and Register based architecture is good to have and will come in handy when we’ll talk about ART vs DVM in android as well as JVM vs DVM in the next section.
Java Virtual Machine is a virtual machine capable of running the java bytecode independent of the underlying platform. Java bytecode can be run on any machine capable of running JVM.
Here is the architecture of JVM:
DVM (Dalvik Virtual Machine) was created by Dan Bornstein and his team, keeping in mind the constraints of a mobile device. It was a purpose specific VM and was strictly created for mobile devices.
Here is a paper by David Ehringer that explains the Architecture of Dalvik Virtual Machine.
The contrasting points of DVM with JVM are:
It uses a register based architecture.DVM is capable of running .dex files produced by converting .class files using a dex tool.It also uses JIT compiler.
This section is very crucial in understanding the comparison of ART vs DVM in android. The main contrast in ART vs DVM is that ART uses AOT compilation whereas DVM uses JIT compilation.
More recently, ART has started using a hybrid of AOT and JIT. We’ll look into that in the later sections.
So there are the basic differences between JIT and AOT compilation processes. Now with this knowledge in the bank we’re ready to take a look at ART vs DVM in Android.
DVM was designed specifically for mobile devices and was used as a virtual machine for running android apps up until Android 4.4 Kitkat.
Programs for Android are written in java and compiled to bytecode. For DVM this bytecode was eventually converted to .dex or .odex (Optimized Dalvik EXecutable ) files.
But ever since Android 4.4 Kitkat, DVM was replaced by ART (Android Runtime) which uses AOT compilation unlike DVM which used JIT.
Other improvements in ART vs Dalvik in Android can be read here.
Here is an image denoting the difference in Architectures of ART vs DVM
This is the second article in the Android Internals series (here's the first). Let me know what topic you want me to cover next and I’ll be more than happy to write an article on that.
Join the AndroidVille SLACK workspace for mobile developers where people share their learnings about everything latest in Tech, especially in Android Development, RxJava, Kotlin, Flutter, and mobile development in general.
Like what you read? Don’t forget to share this post on Facebook, Whatsapp, and LinkedIn.
Create your free account to unlock your custom reading experience.