I have been thinking to write a blog on something, So I thought now to write about Virtual Machine. Which I have learned DALVIK VM in Linux For You magazine last month and almost all content are copied from that article only J.
Let me start with What is VM?
Linux For You magazine says,
Virtual Machine is an isolated, self-contained implementation that abstracts hardware resources. Typically, it simulates an actual physical machine capable of executing instructions sets. Broadly VM are classified into two types,
- Process VMs
- System VMs
A process VM, sometimes called an application virtual machine, runs as a normal application inside a host OS and supports a single process. It is created when that process is started and destroyed when it exits. Its purpose is to provide a platform-independent programming environment that abstracts away details of the underlying hardware or operating system, and allows a program to execute in the same way on any platform.
This type of VM has become popular with the Java programming language, which is implemented using the Java virtual machine. Other examples include the Parrot virtual machine, which serves as an abstraction layer for several interpreted languages, and the .NET Framework, which runs on a VM called the Common Language Runtime.
Supports the execution of a complete operating system (eg: VirtualBox, VMWare).
System virtual machines (sometimes called hardware virtual machines) allow the sharing of the underlying physical machine resources between different virtual machines, each running its own operating system. The software layer providing the virtualization is called a virtual machine monitor or hypervisor. A hypervisor can run on bare hardware (Type 1 or native VM) or on top of an operating system (Type 2 or hosted VM).
I have learned little bit on system VMs while in my college with helps of my Guru (Ewards). Where with help of System VMs you can run XP in linux, tats wat I tried in my laptop.
In this article, will see on Process based VMs.
For example, Java applets run in a Java virtual machine (VM) that has no access to the host operating system. This design has two advantages:
System Independence: A Java application will run the same in any Java VM, regardless of the hardware and software underlying the system.
Security: Because the VM has no contact with the operating system, there is little possibility of a Java program damaging other files or applications.
The second advantage, however, has a downside. Because programs running in a VM are separate from the operating system, they cannot take advantage of special operating system features.
VM are implemented in two ways,
- Register based VMs
- Stack based VMs
Register based VMs:
It computation model assumes register as the memory, and operands are accesed with explicit address coded in an instruction.
Stack based VMs:
Memory takes the form of a LIFO stack. Every operation boils down into PUSH/POP or STORE/LOAD. These instructions do not need explicit operand address, because the operand is always available with stack pointer. Ex:- Microsoft .NET and Sun JVM.
People preferred to stack-based rather than register-base VMs, main reason of this choice was the simplicity of the complier. And stack-based instruction are shorter of (PUSH/POP). But register-based instructions are quadruple. Even though advantages of stack-based is diminishes in terms of number of instruction, which some of stack-based instructions need to express the register-based instructions.
Example: “a = b + c” would need stack VM:
Stack VMs, operands are accessed relative to the stack pointer, but in register VMs need to preovide the location of all operands. Having all the operands in instruction has it benefits of execution faster compared to stack VM.
Stack VM consume more memory cycle and temp values often stored in main memory. Register VMs, temp values usually remain inregister.
Stack VMs are unable to use few optimisation, in-case of expression, which recalculated each time they appear in the code. Register VM can calculate an expression once, and keep that in a register for all future references.
So Dalvik VM preferred register based VM, and thou google choose to implement it in andriod.
Dalvik VM :
In stead of using usual JVM, Google have implemented their own VM of “Dalvik VM”
The Dalvik virtual machine is a register-based virtual machine, designed and written by Dan Bornstein with contributions from other Google engineers as part of the Android mobile phone platform.
It is optimized for low memory requirements, and is designed to allow multiple VM instances to run at once, relying on the underlying operating system for process isolation, memory management and threading support. Dalvik is often referred to as a Java Virtual Machine, but this is not strictly accurate, as the bytecode on which it operates is not Java bytecode.
License and patent:
Dalvik is claimed by Google to be a clean-room implementation rather than a development on top of a standard Java runtime, which would mean it might not inherit copyright-based license restrictions from either the standard-edition or open-source-edition Java runtimes.
Dalvik is published under the terms of the Apache License 2.0.
On 12 August 2010, Oracle, which acquired Sun Microsystems in April 2009 and therefore owns the rights to Java, sued Google over claimed infringement of copyrights and patents. In developing Android, it is alleged that Google knowingly, directly and repeatedly infringed Oracle’s Java-related intellectual property.
Specifically the patent infringement claim references seven patents including US Patent No. 5966702 “Method And Apparatus For Preprocessing And Packaging Class Files”, and US Patent No. 6910205 “Interpreting Functions Utilizing A Hybrid Of Virtual And Native Machine Instructions”. It also references US Patent No. RE38104 “Method And Apparatus For Resolving Data References In Generated Code”.
The Dex File Format:
In standard Java environments, Java source code is compiled into Java bytecode, which is stored within .class files. The .class files are read by the JVM at runtime. Each class in your Java code will result in one .class file. This means that if you have, say, one .java source file that contains one public class, one static inner class, and three anonymous classes, the compilation process (javac) will output 5 .class files.
On the Android platform, Java source code is still compiled into .class files. But after .class files are generated, the “dx” tool is used to convert the .class files into a .dex, or Dalvik Executable, file. Whereas a .class file contains only one class, a .dex file contains multiple classes. It is the .dex file that is executed on the Dalvik VM.