What is dvm in android

Closer Look At Android Runtime: DVM vs ART

Apr 5, 2017 · 5 min read

Before directly move to Android Runtime, we need to understand what runtime environment is and also understand some basic stuff i.e. the functioning of JVM and Dalvik VM.

What is runtime?

In a simplest term it is a system used by operating system which takes care of converting the code that you write in a high level language like Java to machine code and understand by CPU/Processor.

Runtime comprises of software instructions that execute when your program is running, even if they’re not essentially are a part of the code of that piece of software in particular.

CPUs o r more general term our computers understand only machine language (binary codes) so to make it run on CPU, the code must be converted to machine code, which is done by translator.

So following are the generation of translator in a sequence-

1. Assemblers :

It directly translate assembly codes to machine codes so it was very fast.

2. Compilers :

It translates the code into assembly codes and then use assemblers to translate the code into binary. Using this compilation was slow but execution was fast. But the biggest problem with compiler is that the resulted machine code was platform dependent. In other words the code which runs on one machine may not run on different machine.

3. Interpreters :

It translates the code while executing it. Since the translation happens at runtime, the execution was slow.

How JAVA code execution works?

To maintain the platform independency of the code, JAVA developed JVM i.e. Java Virtual Machine. It developed JVM specific to every platform means JVM is dependency on the platform. The Java compiler converts the .java files into .class files, which is called byte code. This byte code is given to the JVM which converts it into machine code.

This is faster than interpretation but slower than C++ compilation.

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.

Key Point: Dalvik used JIT (Just in time) compilation whereas ART uses AOT (Ahead of time) compilation.

Below are the code snippet explaining the difference between Dalvik Virtual Machine and Java Virtual Machine.

Just In Time (JIT)

With the Dalvik JIT compiler, each time when the app is run, it dynamically translates a part of the Dalvik bytecode into machine code. As the execution progresses, more bytecode is compiled and cached. Since JIT compiles only a part of the code, it has a smaller memory footprint and uses less physical space on the device.

Читайте также:  Автомобильный лаунчер для андроида

Ahead Of Time (AOT)

ART is equipped with an Ahead-of-Time compiler. During the app’s installation phase, it statically translates the DEX bytecode into machine code and stores in the device’s storage. This is a one-time event which happens when the app is installed on the device. With no need for JIT compilation, the code executes much faster.

As ART runs app machine code directly (native execution), it doesn’t hit the CPU as hard as just-in-time code compiling on Dalvik. Because of less CPU usage results in less battery drain.

ART also uses same DEX bytecode as input for Dalvik. An application compiled using ART requires additional time for compilation when an application is installed and take up slightly larger amounts of space to store the compiled code.

Why Android use Virtual Machine?

Android makes use of a virtual machine as its runtime environment in order to run the APK files that constitute an Android application. Below are the advantages:

· The application code is isolated from the core OS. So even if any code contains some malicious code won’t directly affect the system files. It makes the Android OS more stable and reliable.

· It provides cross compatibility or platform independency. It meaning even if an app is compiled on platform such as a PC, it can still be executed on the mobile platform using the virtual machine.

Benefits of ART

· Apps run faster as DEX bytecode translation done during installation.

· Reduces startup time of applications as native code is directly executed.

· Improves battery performance as power utilized to interpreted byte codes line by line is saved.

· Improved garbage collector.

· Improved developer tool.

Drawbacks of ART

· App Installation takes more time because of DEX bytecodes conversion into machine code during installation.

· As the native machine code generated on installation is stored in internal storage, more internal storage is required.

Conclusion

ART is written to run multiple virtual machines on low-memory devices by executing DEX files, a bytecode format designed specially for Android that’s optimized for minimal memory footprint. It makes the UI feel more responsive. That’s all from my side. For more details on ART and Dalvik you can go through official Android document.

Thanks for reading. To help others please click ❤ to recommend this article if you found it helpful.

Check out my blogger page for more interesting topics on Software development.

Источник

Android Core: JVM, DVM, ART, JIT, AOT

It’s really important to understand that how android app works and what components are used to make android device work.

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

  • Why Android use Vitual Machine
  • What is JVM and DVM
  • JVM vs DVM
  • Why Android OS used DVM instead of JVM
  • Why Android depreciated DVM and started to use ART
  • Why Android Reintroducing JIT with ART
  • JIT vs AOT

Why Android use Virtual Machine?

As we are going to know why android use virtual machine. Before that we should a clear idea on Virtual Machine (VM)

What is Virtual Machine (VM)

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.

Читайте также:  Android 11 xiaomi redmi note 9 pro когда выйдет

For example: the JVM runs java bytecode and produces same output on multiple platforms.

Android also makes use of a virtual machine to run the APK files. Below are the advantages:

  • The application code is isolated from the core OS. So even if any code contains some malicious code won’t directly affect the system files. It makes the Android OS more stable and reliable.
  • It provides cross compatibility or platform independency. It meaning even if an app is compiled on platform such as a PC, it can still be executed on the mobile platform using the virtual machine.

Java Virtual Machine | JVM

  • To maintain the platform independency of the code, JAVA developed JVM i.e. Java Virtual Machine
  • JVM is platform dependent and different implementations are available for specific platforms.
  • JVM uses JIT compiler.
  • Java bytecode can be run on any machine capable of running JVM.
  • In JVM, The Java compiler converts the .java files into .class files, which is called bytecode . For example: If we have a Hello.java class and when we run this class file then javac compiler/Java Compiler turns this source code to Hello.class file which is bytecode . Which means javac compiler does not convert Java code directly to machine code like other compiler does.
  • This bytecode is given to the JVM which converts it into machine code.

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

What are you thinking ? as java code execution works by JVM and android uses java. So Android also use JVM?. It would be but though JVM is high performance and provides excellent memory management, it needs to be optimized for low-powered handheld devices as well. Thats why another Virtual machine was designed specifically for mobile devices and that is Delvik Virtual Machine (DVM).

  • DVM is stands for ‘Delvik Virtual Machine’
  • DVM (Dalvik Virtual Machine) was created by Dan Bornstein and his team, keeping in mind only the constraints of a mobile device.
  • It was a purpose specific VM and was strictly created for mobile devices.
  • Similarly to JVM, it also uses the JIT compiler.
  • 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.
  • In DVM, everything is same as JVM except last two steps here. 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.

Note: dexopt tool which is a part of the DVM converts the dex file into .odex file format.

JVM vs DVM

  • JVM is stack based but DVM is register based which is designed to run on low memory.
  • JVM uses java byte code and runs ‘.class’ file having JIT (Just in Time) where DVM uses its own byte code and runs ‘.dex’ file.
  • In JVM, single instance is of JVM is shared with multiple applications where DVM has been designed so that the device can run multiple instance of VM efficiently. Applications are given their own instance.
  • JVM support multiple operating system where DVM supports Android operating system only.
  • In JVM, the executable is JAR where in DVM the executable is APK.
Читайте также:  Как удалить временные файлы андроид

Why Android OS uses DVM instead of JVM?

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

  • One of the main reasons of using DVM in android is DVM takes less memory, runs and loads faster compared to JVM
  • 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.

Why Android depreciated DVM and started to use ART ?

There were some drawbacks to use DVM. In Dalvik runtime , an app is compiled every time when an app is launched. This leads to excessive CPU and memory usage and also reduces the battery life. Thats why from Android 4.4 (KitKat) ART was introduced and from Android 5.0 (Lollipop) it has completely replaced Dalvik.

  • ART stands for ‘Android Run-Time’
  • ART uses AOT (Ahead-of-Time) compilation

In ART, During the app’s installation phase, AOT ( Ahead-of-Time) statically translates the DEX bytecode into machine code and stores in the device’s storage. This is a one-time event and happens only when the app is installed on the device. This leads to better battery life and great performance. So there’s no need for JIT compilation, and the code executes much faster.

The most significant change from Dalvik to ART is that Dalvik is based on Just-in-Time (JIT) compilation, while ART is based on Ahead-of-Time (AOT) compilation.

  • AOT uses dex2oat tool to compile .dex files and generates a compiled app to be run on the target device
  • Garbage collection is one of the main reasons for a poor UX, poor responsiveness, and ultimately bad reviews. In the Dalvik days, GC used to have two passes over the heap, which led to poor UXs. This situation is improved in ART, as only one pass over the heap to consolidate the memory is required.

Benefits of ART over DVM

  • Apps run faster as DEX bytecode translation done only during installation.
  • Reduced startup time of applications as native code is directly executed.
  • Improved battery performance as power utilized to interpreted byte codes line by line is saved.
  • Improved garbage collector
  • Improved developer tool.

Reintroducing of JIT with ART

Though ART improved app performance, there were some drawbacks of ART.

Drawbacks of ART

  • App Installation takes more time because of DEX bytecodes conversion into machine code during installation.
  • As the native machine code generated on installation is stored in internal storage, more internal storage is required.

To tackle these problems such as initial installation time and memory from Android Nougat JIT (Just In Time ) Compilation was reintroduced with code profiling along with AOT, and an interpreter in the ART thus making it hybrid. Using the Hybrid Runtime, there won’t be any compilation during install, and applications can be started right away, the bytecode is interpreted.
Now with ART and the new JIT the application installation is faster. The new JIT constantly does profiling and improves applications as they run.

JIT vs AOT

  • JIT (Just In Time) compiler compiles source code into machine code during runtime (This means that each time your app is run, a part of a .dex file is converted dynamically ) where AOT Compiles the source code before execution.
  • JVM and DVM uses JIT where ART uses AOT Compliler

Be sure to give claps if you find this article helpful. Happy Coding !

Источник

Оцените статью