JVM vs DVM

In this article, we’ll see the differences between Java Virtual Machine and Dalvik Virtual Machine

Himanshu Verma
Towards Data Science

--

Photo by patricia serna on Unsplash

When I was learning Java programming seven years ago from now, I used to ask a lot of questions from myself and friends about this marvellous language but there was one question which popped up most of the time until I didn’t find the satisfactory answer. The question was why java bytecode can run on any machine or platform and how it is different from the other code?. The answer to this question was not easy for me at that time because I was just getting started.

So in this guide, I’m going to answer the following questions

  • JVM vs DVM
  • Why Android OS uses DVM instead of JVM?

Java Virtual Machine

A Java virtual machine (JVM) is an abstract layer between the Java program and the platform the Java code is running on. JVM is platform dependent and different implementations are available for specific platforms.

For example, If you have a Hello.java class and when you run this class file then javac compiler turns your source code to bytecode and creates Hello.class file which means javac compiler does not convert Java code directly to machine code like other compiler does. Bytecode is intermediate code which means humans can not understand this code and this code is not machine/platform dependent. Since bytecode is an intermediate code so you can give it to anyone to run it on any platform that’s why we call Java applications WORA (Write Once and Run Anywhere).

Image credit to netjs

As you can see from the image above, once you have .class file ready then you can give this file to any platform and it will convert it to native machine code.

Dalvik Virtual Machine

The Dalvik Virtual Machine (DVM) is a virtual machine which executes the android applications. Since everything in mobiles is very limited whether it would be a battery life, processing and memory etc. It had been optimised so that it can fit in with low-powered devices.

Image Credit to Tam H. Doan

As you can see from the image above, everything is same as JVM except the last two steps. 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.

JVM vs DVM

One of the main reasons of using DVM in android is because it follows the register based model and it is much faster than stack based model while JVM follows the stack based model which takes a lot of memory and also slower than DVM.

There are some major differences, so let’s have a look them

Credit to aatul.me

Why Android OS uses DVM instead of JVM?

There are couple of reasons why Google not choose JVM over DVM, so let’s understand each one of them one by one.

  • Though JVM is free, it was under GPL license, which is not good for Android as most the Android is under Apache license.
  • JVM was designed for desktops and it is too heavy for embedded devices.
  • DVM takes less memory, runs and loads faster compared to JVM.

The OHIO State University

Conclusion

JVM will work based on byte code and the DVM will work based on optimized bytecode, it is optimised for mobile platforms because mobile devices have less memory, low process and low power that’s why it is using the linux kernal.

I hope you liked reading this article, you can also visit my website where I keep posting article regularly.

Subscribe my mailing list to get the early access of my articles directly in your inbox and Don’t forget to follow my own publication on Medium The Code Monster to polish your technical knowledge.

Know your author

Himanshu Verma has graduated from the Dr. APJ Abdul Kalam University, Lucknow (India). He is an Android & IOS Developer and Machine Learning & Data Science Learner, a Financial Advisor, and a Blogger.

--

--

Visit my website http://thehimanshuverma.com/. Android & IOS Developer | Researcher | ML & Data Science Enthusiastic | Blogger | FA