Professional Documents
Culture Documents
L11 Android Virtual Machine
L11 Android Virtual Machine
<date/time> <footer> 1
Outline
Virtual Machine
Java Virtual Machine
Dalvik Virtual Machine
06/14/2024 2
Concept
A virtual machine (VM) is a software emulation of a
real machine so that
another operating system can run in the simulated machine
more than one different operating systems can run at the same
time
Fundamental idea: abstract the real hardware into
different execution environments
06/14/2024 3
VM Components
Three components of a virtual machine
Host: underlying hardware system
Virtual machine manager (VMM): creates and runs different
virtual machines
Guest: the emulated operating system on the host system
06/14/2024 4
VM Components (2)
Guest
VMM
Host
06/14/2024 5
System/Process VM
Two major kinds of virtual machines based on their
emulation degree of real machines
System 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
06/14/2024 6
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
06/14/2024 7
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.
06/14/2024 8
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
06/14/2024 9
Java Bytecode
Java bytecode is the instruction set for Java virtual
machine
Each bytecode consists of two parts
One or two bytes that represent the instruction
Zero 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
06/14/2024 10
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
06/14/2024 11
JVM Workflow(2)
A.java Java Compiler A.class
Compile source code
Class Loader
API
va Bytecode Interpreter
J a
B.class
Host system
(Windows, Linux, etc)
06/14/2024 12
JVM Components (1)
Class loader
Loads .class file into memory
Verifies bytecode instructions
Allocates memory for the program
06/14/2024 13
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
06/14/2024 14
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
06/14/2024 15
JVM Architecture
06/14/2024 16
Java Runtime Environment
We usually install the
Java Runtime
Environment (JRE)
on our computers to
use the Java platform
JRE consists of the
JVM and Java APIs
06/14/2024 17
Android Virtual Macihne
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
06/14/2024 18
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
06/14/2024 19
Android Architecture
Virtual
Machine
C/C++
06/14/2024 20
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
06/14/2024 21
Dalvik Bytecode VS Java Bytecode
06/14/2024 22
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
06/14/2024 23
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
06/14/2024 24
Java Source Code to Android App
Java
Compiler Java Bytecode
Java Source
.class file
.class file
Code .class file
.class file
(.java files) .class file
dx tool
Dalvik Bytecode
Resource files
.dex file
Package Builder
Android App
.apk file
06/14/2024 25
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
06/14/2024 26