Is a Dalvik virtual machine instance created for each application? - android

Is a Dalvik virtual machine instance created for each application, or all Android applications share the same Dalvik virtual machine instance?

From the developer docs:
Every Android application runs in its own process, with its own
instance of the Dalvik virtual machine. Dalvik has been written so
that a device can run multiple VMs efficiently.
The Dalvik VM executes files in the Dalvik Executable (.dex) format
which is optimised for minimal memory footprint.
The VM is register-based, and runs classes compiled by a Java language
compiler that have been transformed into the .dex format by the
included dx tool.
Also have a look at What is... The Dalvik Virtual Machine for detailed description about DVM.

The Dalvik virtual machine is built specifically for Android. It was built to address the battery life and processing power problems, and it is free.
We are using Dalvik VM instead of Java Virtual Machine (JVM) because the Java compiler, the Java tools are free, but the JVM is not free, so the Android developers from Google have made their own virtual machine, and made it as free.
A virtual machine is necessary because the virtual machine helps in debugging, as a virtual computer so that my applications can run different devices the same way
Pictorial Representation

A .java file is given to the java compiler (javac) to generate the .class file.
All .class files are given to dx tool to generate a single dex file.
The dex file is given to the Dalvik VM to generate the final machine code.
The final machine code is given to the CPU to execute.

All apk's basic source code is in java language . When you build this project all .java files get converted to .class now the dx tool of adk converts all .class files to classes.dex file .And this classes.dex file is executed on dalvik virtual machine.
For more info on dalvik virtual machine: http://www.slideshare.net/jserv/understanding-the-dalvik-virtual-machine
Dalvik virtual machine is intended to run multiple VMs at a time .
So every app runs in its own process, with its own instance of the Dalvik virtual machine as said by #sahilMahajanMj .
And this classes.dex file is further optimized to odex file and saved in /dalvik/dalvik-cache
To know more about odex click this .
If you want to know why DVM for android why not JVM click this

Dalvik is a virtual machine where every android application runs. Through Dalvik, device is able to run multiple virtual machines through better memory management as Dalvik VMs are register based and hence memory efficient
Every android app runs in its own process, with its own instance of Dalvik VM.
First, Java files are connverted to .class file by java compiler
.class files are given to "dx" tool which converts it to .dex format
.dex file is given to DVM to produce machine code
Machine code is executed by CPU
.apk file contains .dex file in zip format which can be run on Dalvik VMs
Why Dalvik ?
1. DVM are built for battery life, processing power and its free
2. We're using DVM instead of JVM as JVM is not free
Dalvik VMs gives consistency across all the mobile devices i.e. one application will run across different devices in the same way

Related

Android app is native or JavaVM code?

Probably an stup!## question.
I dont know nothing about developing apps for Android, but I was wondering if the applications for Android, when they are compiled, the code generated is PURE ANDROID NATIVE CODE, or is Java Virtual Machine compatible code.
Thanks a lot.
It has its own virtual machine, Dalvik.
Most apps for Android are Java apps. They are compiled to .class files by the Java compiler, then the dx compiler takes the .class and compiles them to .dex files, which are executable by the Dalvik VM on Android (which is the Java VM).
Although the Dalvik VM is made to run Java code, it is significantly different than the standard Java VM on your computer. The standard VM is a "stack based" machine, whereas the Dalvik VM is "register based".
You can also make apps with the NDK, and write them in C or C++. This is not Java compatible in any way (although you can interface the native C code with the Java code with JNI).
There are at least three ways to do it:
All Java
All Native
Half Java, half native. Android libraries can be written in C and compiled into native code, then called from Java. Your application could be a mix of both, if you wish.
More info: http://en.wikipedia.org/wiki/Android_software_development#Native_development_kit

Why does Android need a Virtual Machine(DVM)?

Virtual Machines are used for running apps on different OS(Java compiles code into OS independent .class files which is interpreted into bytecode). Since android apps run on the same OS (i.e Android), it doesn't require a VM (I could be wrong here). Then why does it need a Dalvik VM?
Android Platform can run on different architectures - ARM, MIPs and x86. To abstract out the need to compile binaries for each architecture VM comes into play. It also does memory management for the developer.
We need someone to compile and convert the java classes into bytecode which can be executed by the interpreter.
It is similar to JVM ... you have .java files which will be compiled by java compiler into .class files. the .class files are nothing but bytecode which will be run by the JVM. JVM can reside on any platform(windows,linux or unix).
In android too the files are compiled into .dex files and run by DVM. just to give an idea, when is application is installed, the Android OS assigns unique linux user id, a DVM is assigned for each app. So in short each app has own linux process, DVM and linux user id.
The java files are compiled into .dex files which consume less memory compared to .class files.
Now assume 10 applications are having 10 individual DVM's and the OS has 10 process to handle.
The dispatcher or scheduler in the android OS is responsible for handling these 10 processes....which is why we have android activity life cycle.
You need DVM to maintain the running state of each process(each app).
Why android needs a virtual machine is on the basis that Google engineered Android API to vastly use a Java interface. Java itself is usually run on a virtual machine.
The JVM itself is a stack machine based VM while Android's VM (called Dalvik) is a register based VM (this is for the sake of less code generated and faster speed to get better performance out of whatever device is using Android)
The purpose of a virtual machine is to be able to abstract hardware by simulating it. If you make a virtual machine and compile it to work on every possible hardware, you get what originally made Java rise to popularity: write once run anywhere portability.
You could write code, without having to change it and run it on any hardware that your virtual machine can run on.
Digressing, Android is mostly built in C (and C++?) but the API that manipulates the OS is interfaced through Java, thus you require a virtual machine.

Is there any way to rebuild the Dalvik interpreter and run it without invoicing a full VM?

I want to rebuild the Dalvik interpreter to add some security features to protect my code. Is there any way to run only a Dalvik interpreter without invoking a full Dalvik virtual machine?

What is Android and why did they create a new VM?

Android's embedded OS relies on the
Linux kernel for core system services
but is not embedded Linux and thus
standard utility libraries like for
example GNU C are not supported. The
Java framework is used to write
Android applications but Android is
not Java. Standard Java libraries such
as Swing are not supported.Android has
a Virtual Machine known as Dalvik, so
when the Java class files are
generated, this are translated to
Dalvik Executable files known as .dex
files. From this point this files are
not treated as java bytecode but as
.dex files.
So I have three questions:
Now I know what Android is not,
so, what is Android?
What are
core system services?
Why did they choose .dex files
upon Java bytecode? I mean why did
they took the work to translate or
combine .class files to .dex files?
The What is Android? article on the developer site describes best what Android is.
Why did they choose .dex files upon
java bytecode? i mean why did they
took the work to translate or combine
.class files to .dex files?
That is because they needed a more efficient virtual machine. Their virtual machine called DalvikVM was optimized for low memory requirements.
You can read more about the DalvikVM in this Wikipedia entry. Make sure to checkout the external links section on that page.

DalvikVM Vs JavaVM in Android?

In General, Android runs the each App as a seperate process in Dalvik Vm. I got this from the Doc. But i can not understand what is the main reason to go to Dalvik VM for Android. What are the Advantages it has than Java VM. Share your Knowledge. It helps.
Thanks in Advance.
A few differences that i found...
Dalvik Vs JVM
Architecture Register Stack
OS Support Android Multiple
Re- Tools few many
Executables APK JAR
Constant-Pool Per Application Per class
In Addition to this
Dalvik has the capacity to compress resources that you have in your application there by reducing the final apk size and makes the device run multiple instances of the VM efficiently
The VM was slimmed down to use less space
Optimized for minimal memory footprint.
From Android 2.2 SDK Dalvik has got a Just in Time compiler
Regarding Licenses
Dalvik is said to be a clean-room implementation rather than a development on top of a standard Java runtime, which would mean it does not inherit copyright-based license restrictions from either the standard-edition or open-source-edition Java runtimes. Dalvik is published under the Apache 2 license. (Source: wikipedia)
You can also read more information regarding the same on the following links
http://code.google.com/events/io/2010/sessions/jit-compiler-androids-dalvik-vm.html
http://en.wikipedia.org/wiki/Dalvik_%28software%29
http://2009.confidence.org.pl/materialy/prezentacje/marc_schoenefeld_reconstructing_confidence_2009.pdf
Dalvik VM is used in system specially in embedded systems where memory is low and processing speed of processor is not high.
Dalvik uses dex files to execute which is converted and zipped version of class files.It is very very small in size roughly less or equal of compressed jar file of same class files.

Categories

Resources