Mobile Handset Virtual Machine. Outline Virtual Machine Java Virtual Machine Dalvik Virtual Machine...

40
Mobile Handset Virtual Machine

Transcript of Mobile Handset Virtual Machine. Outline Virtual Machine Java Virtual Machine Dalvik Virtual Machine...

Page 3: Mobile Handset Virtual Machine. Outline Virtual Machine Java Virtual Machine Dalvik Virtual Machine 2.

3

Concept

A virtual machine (VM) is a software emulation of a real machine so thatanother operating system can run in the simulated

machinemore than one different operating systems can run

at the same time Fundamental idea: abstract the real hardware

into different execution environments

Page 4: Mobile Handset Virtual Machine. Outline Virtual Machine Java Virtual Machine Dalvik Virtual Machine 2.

4

VM Components (1)

Three components of a virtual machineHost: underlying hardware systemVirtual machine manager (VMM): creates and runs

different virtual machinesGuest: the emulated operating system on the host

system

Page 6: Mobile Handset Virtual Machine. Outline Virtual Machine Java Virtual Machine Dalvik Virtual Machine 2.

6

System/Process VM

Two major kinds of virtual machines based on their emulation degree of real machinesSystem virtual machine: it provides a complete

system platform to support a complete operating system

Process virtual machine: it runs a single program and supports a single process

Page 7: Mobile Handset Virtual Machine. Outline Virtual Machine Java Virtual Machine Dalvik Virtual Machine 2.

7

System VM

It usually emulates a platform to run programs where the real hardware is not available to use (for example, running Mac OS on a windows computer)

Multiple OSes can co-exist on the same physical hardware and are strongly isolated from each other

Popular virtual machine manager products: Xen, VMware Workstation, Sun Virtualbox

Page 8: Mobile Handset Virtual Machine. Outline Virtual Machine Java Virtual Machine Dalvik Virtual Machine 2.

8

Process VM

It is created when the process is started and destroyed when the process exits. Its purpose is to provide a platform-independent programming environment so that a program can execute in a same way on any platform

For example, Java bytecode can run on any platform where there is a Java Virtual Machine and the code does not need to be recompiled.

Page 10: Mobile Handset Virtual Machine. Outline Virtual Machine Java Virtual Machine Dalvik Virtual Machine 2.

10

Java Virtual Machine

Java Virtual machine (JVM) is the virtual machine that can execute Java bytecode. It is the execution part of the Java platform

It helps Java to achieve its goal “write once, run anywhere” by hiding the differences between different operating systems

Page 11: Mobile Handset Virtual Machine. Outline Virtual Machine Java Virtual Machine Dalvik Virtual Machine 2.

11

Java Bytecode

Java bytecode is the instruction set for Java virtual machine

Each bytecode consists of two partsOne or two bytes that represent the instructionZero or more bytes for parameters

Java compiler compiles Java code into Java bytecode. The Java programmer does not need to be aware of or understand Java bytecode

Page 12: Mobile Handset Virtual Machine. Outline Virtual Machine Java Virtual Machine Dalvik Virtual Machine 2.

12

Example of Java Bytecode

Java Compiler

Java CodeJava Bytecode

instruction Fucntion

iconst_n Push the integer constant n onto the stack

istore_n Store an integer value into the variable of index n

iload_n Load an integer value from variable of index n

sipush Push a short integer onto the stack

iinc n i Increase variable of index n by i

Page 13: Mobile Handset Virtual Machine. Outline Virtual Machine Java Virtual Machine Dalvik Virtual Machine 2.

13

JVM Workflow (1)

Java source code is compiled into Java bytecode which is stored within .class files

Each class in Java source code will be compiled into one .class file.

The .class files are read and interpreted by JVM

Page 16: Mobile Handset Virtual Machine. Outline Virtual Machine Java Virtual Machine Dalvik Virtual Machine 2.

16

JVM Components (2)

Runtime data area Method area: it stores class and method codes Heap: it is the place where Java objects are created Java stacks: they are places where Java methods are

executed Program counter (PC) registers: they store memory

addresses of the instructions which to be executed Native method stacks: they are places where native

methods (e.g. C programs) are executed. Native method is a function which is written in another language other than Java

Page 17: Mobile Handset Virtual Machine. Outline Virtual Machine Java Virtual Machine Dalvik Virtual Machine 2.

17

JVM Components (3)

Native method interface: it is a program that connects native method libraries with JVM for executing native methods

Native method library Execution engine: it contains the interpreter

which converts Java bytecode into machine code

Page 21: Mobile Handset Virtual Machine. Outline Virtual Machine Java Virtual Machine Dalvik Virtual Machine 2.

21

Introduction

Dalvik virtual machine (DVM) is the process virtual machine in Google’s Android operating system. It executes applications written for Android.

It is open-source software which was originally written by Dan Bornstein, who named it after the fishing village of Dalvik in Iceland

Page 22: Mobile Handset Virtual Machine. Outline Virtual Machine Java Virtual Machine Dalvik Virtual Machine 2.

22

Why DVM not JVM

When Google selected Java as the language for developing Android applications, it chose DVM instead of JVM for several reasons: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

Page 24: Mobile Handset Virtual Machine. Outline Virtual Machine Java Virtual Machine Dalvik Virtual Machine 2.

24

Dalvik Bytecode

Dalvik bytecode is the instruction set for Dalvik virtual machine

Android programs are firstly compiled into Java bytecode which is in .class files. A tool called dx then converts Java bytecode into Dalvik bytecode

Page 27: Mobile Handset Virtual Machine. Outline Virtual Machine Java Virtual Machine Dalvik Virtual Machine 2.

27

DEX File

A .dex (Dalvik EXecutable) file is used to store the Dalvik bytecode. It is converted from .class files and executed on the DVM

The .dex file has been optimized for memory usage and the design principle is sharing of data

It uses shared, type-specific constant pools as its primary mechanism for saving memory

Page 28: Mobile Handset Virtual Machine. Outline Virtual Machine Java Virtual Machine Dalvik Virtual Machine 2.

28

Constant Pool Concept

A constant pool is a table stores all constant values (e.g. string constants, field constants, class constants, etc.) used within a Java class.

Constant values are referred to by their index in the constant pool rather than stored throughout the class

Constant pool is the biggest part of the Java class file and takes up 61% of the file size

Page 29: Mobile Handset Virtual Machine. Outline Virtual Machine Java Virtual Machine Dalvik Virtual Machine 2.

29

Constant Pool Optimization (1)

Although average size of one .class file is quite small, the size still matters because the time to read file from storage is a dominant factor in VM startup time

.dex file optimizes the constant pool when converted from .class files

Page 30: Mobile Handset Virtual Machine. Outline Virtual Machine Java Virtual Machine Dalvik Virtual Machine 2.

30

Constant Pool Optimization (2)

In the .class file, each class has its own private, heterogeneous constant pool. It is heterogeneous because all types of constants (field, string, class, etc.) are mixed together

In the .dex file, all classes share the same type-specific constant pools. Duplication of constant values across different classes is eliminated

Page 31: Mobile Handset Virtual Machine. Outline Virtual Machine Java Virtual Machine Dalvik Virtual Machine 2.

31

Constant Pool Optimization (3)

Java Source Code

(.java files)

Heterogeneous Constant Pool

Other Data

.class

Heterogeneous Constant Pool

Other Data

.class

Heterogeneous Constant Pool

Other Data

.class

Strings Constant Pool

Other Data

.dex

Class Constant Pool

Field Constant Pool

Method Constant Pool

Java Compiler

dx tool

Page 32: Mobile Handset Virtual Machine. Outline Virtual Machine Java Virtual Machine Dalvik Virtual Machine 2.

32

Memory Saving Evaluation

The Android team found that .dex file format cuts the size in half of some common system libraries and applications within Android systemCode Size of .class Files

(bytes)Size of .dex File (bytes)

Common System Libraries 21,445,320 (100%) 10,311,972 (48%)

Web Browser App 470,312 (100%) 209,248 (44%)

Alarm Clock App 119,200 (100%) 53,020 (44%)

Page 33: Mobile Handset Virtual Machine. Outline Virtual Machine Java Virtual Machine Dalvik Virtual Machine 2.

33

Zygote (1)

Zygote is another concept used by Android to speedup VM performance

Every Android application runs in its own instance of the VM, so VM instances should be able to start quickly when a new application is launched

Zygote enables code sharing across different VM instances and provide fast startup time for new VM instances

Page 34: Mobile Handset Virtual Machine. Outline Virtual Machine Java Virtual Machine Dalvik Virtual Machine 2.

34

Zygote (2)

Zygote is a VM process which starts at system boot time. When Zygote starts, it initializes a VM instance and preloads core library classes which are good candidates for sharing across processes

Zygote will sit and wait for socket requests from other processes who need new VM instances

Cold starting VM takes a long time. Once a request occurs, Zygote will fork a new VM instance from itself and the startup time will be minimized

Page 36: Mobile Handset Virtual Machine. Outline Virtual Machine Java Virtual Machine Dalvik Virtual Machine 2.

36

Android Runtime

Android Runtime (ART) is an application runtime environment used by Android operating system

It is designed to replace Dalvik virtual machine. It supports standard .dex file to maintain backward compatibility

ART came out as an alternative runtime environment in Android 4.4

Dalvik virtual machine was entirely replace by ART in Android 5.0

Page 37: Mobile Handset Virtual Machine. Outline Virtual Machine Java Virtual Machine Dalvik Virtual Machine 2.

37

Java Source Code to Android App

Java Source Code

(.java files)

Dalvik Bytecode.dex file

Java Compiler

dx tool

.class file.class file

.class file.class file

.class file

Java Bytecode

Resource files

Android App.apk file

Package Builder

Page 38: Mobile Handset Virtual Machine. Outline Virtual Machine Java Virtual Machine Dalvik Virtual Machine 2.

38

Android Application Launch Procedure Many things happen in the background when a user

clicks on an icon and launch a new application The click event gets routed to activity manager. The activity manager sends parameters to Zygote process

over the socket connection and creates a new process Zygote forks itself and returns the new process ID The activity manager attaches the new process to the

application and the application’s classes will be loaded into the process’s memory

The application is launched

Page 39: Mobile Handset Virtual Machine. Outline Virtual Machine Java Virtual Machine Dalvik Virtual Machine 2.

39

References (1) http://en.wikipedia.org/wiki/Virtual_machine http://www.virtual-managed-servers.com/process-virtual-machine.html http://www.virtual-managed-servers.com/system-virtual-machine.html http://courses.mpi-sws.org/os-ss11/lectures/proc9.pdf http://en.wikipedia.org/wiki/Java_virtual_machine http://www.utdallas.edu/~

muratk/courses/cloud11f_files/smith-vm-overview.pdf www.cse.sc.edu/~rose/311/ppt/ch16.ppt http://web.cs.wpi.edu/~cs502/s06/LectureNotes/ http://www.javaservletsjspweb.in/2012/02/java-virtual-machine-jvm-archit

ecture.html#.VOupzfnF9t8

http://skillgun.com/question/436/android/basics/what-is-the-difference-between-dvm-and-jvm-why-android-opted-for-dvm