Why does Android need a virtual machine (DVM)?

Virtual machines are used to run applications on different OSes (Java compiles the code into OS independent .class files, which are interpreted to bytecode). Since Android apps run on the same OS (like Android), it doesn't require a virtual machine (I could be wrong here). Then why do you need Dalvik VM?


source to share

3 answers

Android Platform can run on different architectures - ARM, MIP and x86. To abstract the need to compile binaries for each architecture, the VM comes into play. It also does memory management for the developer.



We need to compile and convert the java classes to bytecode that the interpreter can execute.

It is similar to JVM ... you have .java files to be compiled by java compiler into .class files. .class files are nothing more than bytecode that the JVM will run. JVM can be on any platform (windows, linux or unix).

In android too, files are compiled into .dex files and DVM is launched. just to give an idea, when the app is installed, android OS assigns a unique linux user ID, DVM is assigned for each app. In short, each application has its own linux process, DVM and linux user id.

java files are compiled into .dex files which consume less memory compared to .class files.

Now let's say 10 applications have 10 separate DVMs and the OS has 10 processes to process.

The dispatcher or scheduler in Android OS is responsible for handling these 10 processes ... which is why we have the Android Activity Lifecycle.

You need DVM to maintain the current state of each process (each application).



Why does android need a virtual machine based on what Google has developed Android API to use Java interface. Java itself usually runs in a virtual machine.

The JVM itself is a stack-based virtual machine, while the Android VM (called Dalvik) is a register-based virtual system (that's for the sake of less generated code and faster speed to improve the performance of any device using Android)

The goal of a virtual machine is to be able to abstract the hardware by simulating it. If you create a virtual machine and compile it to run on every possible hardware, you get what originally made Java popular: write as soon as you run any portability.

You can write code without changing it and run it on any hardware your virtual machine can run on.

As a side note, Android is mostly built in C (and C ++?), But the API that drives the OS communicates through Java, so you need a virtual machine.



All Articles