Site icon AndroidVille

Android Internals: ART vs DVM in Android

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.

What is a Virtual Machine?

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:

We’ll be looking at the two ways of implementing a Virtual Machine

 

Stack Based Architecture

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:

  1. POP 20
  2. POP 7
  3. ADD

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 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:

  1. ADD R1, R2, R3

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.

 

JVM vs DVM

 

JVM

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

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:

 

AOT vs JIT

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.

JIT

AOT

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.

 

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.

ART

Other improvements in ART vs Dalvik in Android can be read here.

DVM

Here is an image denoting the difference in Architectures of ART vs DVM

Conclusion

This is the second article in the Android Internals series. Let me know what topic you want me to cover next and I’ll be more than happy to write an article on that.

 

*Important*: 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.

Click on this link to join the workspace. It’s absolutely free!

Like what you read? Don’t forget to share this post on FacebookWhatsapp, and LinkedIn.

You can follow me on LinkedInQuoraTwitter, and Instagram where I answer questions related to Mobile Development, especially Android and Flutter.

If you want to stay updated with all the latest articles, subscribe to the weekly newsletter by entering your email address in the form on the top right section of this page.

Exit mobile version