Introduction to Java : Feature to Java, Java Virtual Machine, Differences between C++ and Java,
Part of Java, API Document, Starting a Java Program. Important Classes, Formatting the Output
Objective of this article is to share internal architecture details of Java Virtual Machine. Focuses on:
- How many component does JVM has?
- How these component are integrated?
- How processing takes place at run time for classes?
Introduction to Java : Feature to Java, Java Virtual Machine, Differences between C++ and Java,
Part of Java, API Document, Starting a Java Program. Important Classes, Formatting the Output
Objective of this article is to share internal architecture details of Java Virtual Machine. Focuses on:
- How many component does JVM has?
- How these component are integrated?
- How processing takes place at run time for classes?
Infocampus is one of the best institute that provides Advanced Java Training in Bangalore with the live project. Training on Java Course is provided in lesser fees and payment installament facility is available for java training.Learn OOPs concept, Packages,Spring, Servlet , Hibernate etc in core & advanced java classes. Join Infocampus and get 100% job support. Infocampus provides java/j2ee classes by the more than 10 years experienced trainers. For the best core java training ,mock test is conducted every weekend.
For the free demo classes call at: 9738001024 or to know about the syllabus for the java training enquire at : http://www.infocampus.co.in/java-training-bangalore.html
Infocampus is one of the best institute that provides Advanced Java Training in Bangalore with the live project. Training on Java Course is provided in lesser fees and payment installament facility is available for java training.Learn OOPs concept, Packages,Spring, Servlet , Hibernate etc in core & advanced java classes. Join Infocampus and get 100% job support. Infocampus provides java/j2ee classes by the more than 10 years experienced trainers. For the best core java training ,mock test is conducted every weekend.
For the free demo classes call at: 9738001024 or to know about the syllabus for the java training enquire at : http://www.infocampus.co.in/java-training-bangalore.html
Similar to Mobile Application Development- Configuration and Android Installation (20)
2. Creating a New Project
• How do Android Apps Work?
• There are different ways the programs run on various platforms. The lowest
level software can be written in machine code that runs directly on the
microprocessor. This is shown in Figure below
• Since it is difficult to develop complex applications in machine code, operating
systems are used.
3. Creating a New Project
• How do Android Apps Work?
• Operating systems provide a communication and control layer between the
application software and the hardware as shown in Figure below
4. Creating a New Project
• How do Android Apps Work?
• If we want to develop a native application for running on a specific hardware/operating system, we have
to do this using a compiler and linker.
• Compiler and linker takes the source code and creates the executable file that actually runs on the
operating system as shown in Figure below.
• For example, if we want to develop an application in C++ programming language, we have to utilize the
compilation/linking process.
• The main advantage of native applications is their speed.
• However, the disadvantage is the incompatibility across different platforms.
• For example, we cannot run a native Windows application on Ubuntu and vice versa.
5. Creating a New Project
• How do Android Apps Work?
• Virtual machine concept is developed to overcome this limitation.
• Virtual machine is software that runs on the operating system and provides an abstraction
to the developer as shown in Figure below.
• The application software runs on top of the virtual machine
6. Creating a New Project
• How do Android Apps Work?
• Therefore, as long as a computer has the virtual machine running, the application software can run on
that computer independent of the hardware and the operating system.
• A good example is the Java Virtual Machine (JVM). JVM runs on almost all operating systems and
platforms.
• Therefore, when we develop Java software, it will be run onthe JVM independent of the operating
system/platform.
7. Creating a New Project
• How do Android Apps Work?
• The obvious advantage of developing apps that run on virtual machines can then be stated
as: “develop once and run on all platforms”.
• However, applications running on virtual machines are slower than native applications.
• General development process of virtual machine applications is summarized in Figure
8. Creating a New Project
• How do Android Apps Work?
• The obvious advantage of developing apps that run on virtual machines can then be stated
as: “develop once and run on all platforms”.
• However, applications running on virtual machines are slower than native applications.
• General development process of virtual machine applications is summarized in Figure
9. Creating a New Project
• How do Android Apps Work?
• Similar to Java applications, Android applications also run on a JVM.
• There are two special virtual machines used in Android:
• Dalvik Virtual Machine (DVM) and
• Android RunTime (ART).
• These are specialized JVMs which can run on low system resources.
• The .apk files (executables of Android apps) actually run on these virtual machines.
10. Creating a New Project
• How do Android Apps Work?
• DVM has been the default runtime environment (~ virtual machine) until the Lollipop release
(Android 5.0).
• ART is introduced by Android 4.0 and has been the default VM as of Android 5.0.
• DVM and ART basically do the same job: running Android apps independent of the platform.
• The main advantage of ART over DVM is the utilization of a concept called Ahead of Time (AOT)
compilation instead of Just in Time (JIT) approach.
• In AOT, apps are compiled during installation hence they load faster with lower CPU usage.
• On the other hand, JIT compilation provides lower storage space consumption with relatively longer
loading times.
11. What Is the JVM?
• A Virtual Machine is a software implementation of a physical machine.
• Java was developed with the concept of WORA (Write Once Run Anywhere), which runs on
a VM.
• The compiler compiles the Java file into a Java .class file, then that .class file is input into the
JVM, which loads and executes the class file.
• Below is a diagram of the simple working of the JVM.
12. The JVM platform
●
• The compiled code is independent of the architecture of the computer.
• The price to pay is a slower execution.
6
Test.java Test.class
Compiler
Interpreter (JVM)
Interpreter (JVM)
Interpreter (JVM)
13. JVM Architecture Diagram
6
How Does the JVM Work?
The JVM is divided into three main subsystems:
1. ClassLoader Subsystem
2. Runtime Data Area
3. Execution Engine
14. 6
How Does the JVM Work?
1. ClassLoader Subsystem
• It has dynamic class loading functionality.
• It loads, links. and initializes the class file when it refers to a class for the first time at runtime, not
compile time.
• 1.1 Loading
• Classes will be loaded by this component. BootStrap ClassLoader, Extension ClassLoader, and
Application ClassLoader are the three ClassLoaders that will help in achieving it.
1) BootStrap ClassLoader – Responsible for loading classes from the bootstrap classpath, nothing but
rt.jar. Highest priority will be given to this loader.
2) Extension ClassLoader – Responsible for loading classes which are inside the ext folder (jrelib).
3) Application ClassLoader – Responsible for loading Application Level Classpath, path mentioned
Environment Variable, etc.
• The above ClassLoaders will follow Delegation Hierarchy Algorithm while loading the class files.
15. 6
How Does the JVM Work?
1. ClassLoader Subsystem
• 1.2 Linking
• Verify – Bytecode verifier will verify whether the generated bytecode is proper or not if verification
fails we will get the verification error.
• Prepare – For all static variables memory will be allocated and assigned with default values.
• Resolve – All symbolic memory references are replaced with the original references from Method
Area.
• 1.3 Initialization
• This is the final phase of ClassLoading; here, all static variables will be assigned with the original
values, and the static block will be executed.
16. 6
How Does the JVM Work?
2. Runtime Data Area
The Runtime Data Area is divided into five major components:
1. Method Area – All the class-level data will be stored here, including static variables. There is only one method
area per JVM, and it is a shared resource.
2. Heap Area – All the Objects and their corresponding instance variables and arrays will be stored here. There is
also one Heap Area per JVM. Since the Method and Heap areas share memory for multiple threads, the data stored
is not thread-safe.
3. Stack Area – For every thread, a separate runtime stack will be created. For every method call, one entry will be
made in the stack memory which is called Stack Frame. All local variables will be created in the stack memory. The
stack area is thread-safe since it is not a shared resource. The Stack Frame is divided into three subentities:
1. Local Variable Array – Related to the method how many local variables are involved and the
corresponding values will be stored here.
2. Operand stack – If any intermediate operation is required to perform, operand stack acts as runtime
workspace to perform the operation.
3. Frame data – All symbols corresponding to the method is stored here. In the case of any exception, the
catch block information will be maintained in the frame data.
4. PC Registers – Each thread will have separate PC Registers, to hold the address of current executing instruction
once the instruction is executed the PC register will be updated with the next instruction.
5. Native Method stacks – Native Method Stack holds native method information. For every thread, a separate
native method stack will be created.
17. 6
How Does the JVM Work?
3. Execution Engine
The bytecode, which is assigned to the Runtime Data Area, will be executed by the Execution Engine. The Execution
Engine reads the bytecode and executes it piece by piece.
a) Interpreter – The interpreter interprets the bytecode faster but executes slowly. The disadvantage of the
interpreter is that when one method is called multiple times, every time a new interpretation is required.
b) JIT Compiler – The JIT Compiler neutralizes the disadvantage of the interpreter. The Execution Engine will be
using the help of the interpreter in converting byte code, but when it finds repeated code it uses the JIT compiler,
which compiles the entire bytecode and changes it to native code. This native code will be used directly for
repeated method calls, which improve the performance of the system.
1) Intermediate Code Generator – Produces intermediate code
2) Code Optimizer – Responsible for optimizing the intermediate code generated above
3) Target Code Generator – Responsible for Generating Machine Code or Native Code
4) Profiler – A special component, responsible for finding hotspots, i.e. whether the method is called multiple
times or not.
c) Garbage Collector: Collects and removes unreferenced objects. Garbage Collection can be triggered by calling
System.gc(), but the execution is not guaranteed. Garbage collection of the JVM collects the objects that are
created.
d) Java Native Interface (JNI): JNI will be interacting with the Native Method Libraries and provides the Native
Libraries required for the Execution Engine.
e) Native Method Libraries: This is a collection of the Native Libraries, which is required for the Execution Engine.
18. 6
Java/Kotlin Program Execution Steps
on Computer
• By definition, Kotlin is a statically-typed
open-source programming language
generating code that can run on the JVM.
Java/Kotlin Program run on Android Mobile
The main difference between regular Java/Kotlin code
compilation and Android compilation process is that Android
doesn’t work with JVM.
19. 6
Java/Kotlin and Android Together
• Instead Android decided to create two virtual machines
specifically for Android:
1. DVM (Dalvik Virtual Machine)
2. ART (Android Runtime) — introduced with the
release of Android 4.4 (Kitkat), and before it the
runtime environment for Android apps was DVM.
• Regarding their functionalities, ART and Dalvik are
compatible runtimes running Dex byte-code which
can be found in .dex file.
• Now the question arises, from where do we get .dex
file?
• Ans- It turns out, there is one more compiler placed
between Java byte-code (.class file) and DVM. Its
name is DX (DEX compiler) and its purpose is to
convert Java byte-code into Dalvik byte-code.
21. 6
Dalvik Virtual Machine DVM in Android
DVM is stands for ‘Delvik Virtual Machine’
DVM (Dalvik Virtual Machine) was created by Dan Bornstein and his team, keeping in mind only
the constraints of a mobile device.
The Dalvik virtual machine was named by Bornstein after the fishing village “Dalvík” in, Iceland, where
some of his ancestors used to live.
It was a purpose specific VM and was strictly created for mobile devices.
Similarly to JVM, it also uses the JIT Executer.
Since everything in mobiles is very limited whether it would be a battery life, processing and
memory etc. It had been optimised so that it can fit in with low-powered devices.
In DVM, everything is same as JVM except last two steps here. The Dex compiler converts the class
files into the .dex file that run on the Dalvik VM. Multiple class files are converted into one dex file.
22. 6
Dalvik Virtual Machine DVM in Android
Dalvik is “deprecated” and is no longer used after android 4.4 Kitkat.
Note: dexopt tool which is a part of the DVM converts the dex file into .odex file format.
There are 2 types of files:
.dex(Dalvik Executable file) file is an android’s compiled code file. These .dex files are then zipped
into a single .apk file.
.odex file is created by the Android operating system to save space and increase the boot speed of
an Android app (a .apk file).
23. 6
Why Android OS uses DVM instead of JVM?
One of the main reasons of using DVM in android is DVM takes less memory, runs and loads faster
compared to JVM
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.
24. 6
Android Runtime (ART)
With a newer android version specifically from 4.4 version KitKat, there is the concept of ART as an
alternative to DVM.
ART(Android Run Time) is a successor of DVM which uses the same bytecode and .dex files (but not
.odex files), with the succession aiming at performance improvements transparent to the end-users.
Android 5.0 “Lollipop” is the first version in which ART is the only included runtime.
Now the thing that ART does is bring apps that are fully compiled when they’re installed on the device.
Therefore, higher performance as no need to convert code to bytecode then compile.
But the downside is you need more storage space and a little longer to install because of compilation
during installation meaning it has to live on the device all the time.
Hence, instead of relatively small java code, we have larger bytecode/machine code.
You may have heard the terms odexed and de-odexed. What is done in this instance is you take a small
part of the application and then precompile it they can go ahead and make a portion of their
application optimized to run on their device, and so they’ve now precompiled that section of the app
and the rest of its compiled at runtime.
So this makes it just a little faster and more performant than in Dalvik. But this approach takes a little
more storage space.
25. 6
Android Runtime (ART)
The dex2oat is used to optimize and compile .dex into a .oat file which may contain machine code in
the ELF format.
ART compiles apps using the on-device dex2oat tool. This utility accepts DEX files as input and
generates a compiled app executable for the target device.
When an app is installed, Android automatically optimizes app data and creates a corresponding OAT
file.
An OAT file is created by the Android operating system in order to speed up the loading time of an
Android app (.APK file).
26. 6
ART vs. DVM in Android
Android RunTime (ART) Dalvik Virtual Machine (DVM)
ART was designed specifically for mobile devices and
was used as a virtual machine for running Android apps
from Android 4.4 Kitkat onwards.
DVM was designed specifically for mobile devices and was used
as a virtual machine for running Android apps up before
Android 4.4 Kitkat.
ART uses AOT compilation. Uses JIT compilation.
Android Runtime is faster than Dalvik Virtual
Machine
Dalvik is slower in comparison to Android Runtime
AOT uses dex2oat tool to compile .dex files and
generates a compiled app to be run on the target
device.
JIT uses dexopt tool to compile .dex files and generates a
compiled app to be run on the target device.
Android Runtime takes more time to boot, Booting
is slow
Dalvik Virtual Mchine takes less time to boot, Booting is
fast
It generates ELF file as .dex file(it contains native code
also)
It generates Odex file as .dex file
The cache is built at first boot, increasing the
reboot time
The Cache builds up fast over time, reducing the reboot
time
Android Runtime needs more space as it uses
AOT.
Dalvik Virtual Mchine needs less space as it uses Just In
Time compiler
Android Runtime utilizes less battery, thus it has
high battery performance
Dalvik Virtual Mchine utilizes more battery, thus it has low
battery performance
27. 6
ART vs. DVM in Android
Android RunTime (ART) Dalvik Virtual Machine (DVM)
Android Runtime has a better Garbage collection
when compared to DVM
Dalvik Virtual Mchine has a poor Garbage collection when
compared to Android Runtime
Apps here are very responsive and work smoothly. In Android DVM, apps are less responsive in accordance
with Android Runtime
Android Runtime converts the bytecode only once
at the time of installation of application
Dalvik Runtime Virtual Machine converts bytecode every
time the application launches
It is highly experimented and new It is a stable and time-tested virtual machine
It doesn’t have a lot of support from app developers
until now
DVM is the choice of Android developers
It consumes more internal storage space, as it
stores compiled apps in addition to the APKs
DVM works better for lower internal storage devices as
space occupied is less
Android Runtime is the upgraded version of Dalvik
Virtual Machine and comes up with a lot of
improvements
It came prior to Android Runtime and it is replaced with
Android Runtime
28. 6
Android SDK
Android SDK stands for Android Software Development Kit which is developed by Google for Android
Platform.
With the help of Android SDK, we can create android Apps easily.
Android SDK is a collection of libraries and Software Development tools that are essential for
Developing Android Applications.
Whenever Google releases a new version or update of Android Software, a corresponding SDK also
releases with it.
In the updated or new version of SDK, some more features are included which are not present in the
previous version.
Android SDK consists of some tools which are very essential for the development of Android
Application.
These tools provide a smooth flow of the development process from developing and debugging.
Android SDK is compatible with all operating systems such as Windows, Linux, macOS, etc.
30. 6
Android SDK
1. Android SDK tool is an important component of Android SDK.
2. It consists of a complete set of development and debugging tools. Below are the SDK developer
tools:
Android SDK Build tool.
Android Emulator.
Android SDK Platform-tools.
Android SDK Tools.
These are shown below :
Components of Android SDK
31. 6
Android SDK
1. Android SDK tool is an important component of Android SDK.
2. It consists of a complete set of development and debugging tools. Below are the SDK developer
tools:
Android SDK Build tool.
Android Emulator.
Android SDK Platform-tools.
Android SDK Tools.
These are shown below :
1) Android SDK Tools
32. 6
Android SDK
1. Android SDK Build-Tools
Android SDK build tools are used for building
actual binaries of Android App.
The main functions of Android SDK Build tools are,
built
Debug
run and
test Android applications.
The latest version of the Android SDK Build tool
is 30.0.3.
While downloading or updating Android in our
System, one must ensure that its latest version is
download in SDK Components.
Android SDK Tools
33. 6
Android SDK
2. Android Emulator
The Android emulator is an Android Virtual Device
(AVD), which represents a specific Android device.
We can use the Android emulator as a target device
to execute and test our Android application on our
PC.
An Android Emulator is a device that simulates an
Android device on your system.
Suppose we want to run our android application that
we code. One option is that we will run this on our
Android Mobile by Enabling USB Debugging on our
mobile.
Another option is using Android Emulator.
In Android Emulator the virtual android device is
shown on our system on which we run the Android
application that we code.
Android SDK Tools
Thus, it simply means that without needing any
physical device Android SDK component “Android
Emulator” provides a virtual device on the System
where we run our Application.
The emulator’s come with the configuration for
Various android phones, tablets, Wear OS, and Android
TV devices.
34. 6
Android SDK
2. Android Emulator
In Android Virtual Emulator all functions that are
feasible on real Android mobile is works on virtual
Device like:
phone calls, text messages.
stimulate different network speeds.
specify the location of a device
access on google play store and lot’s more.
But there is one disadvantage of this emulator is
that.
It is very slow when System’s PC has less RAM.
It works fine when a maximum GB of RAM is
present on our device.
Android SDK Tools
Requirement and recommendations
SDK Tools 26.1.1 or higher
64-bit processor
Windows: CPU with UG (unrestricted guest) support
HAXM 6.2.1 or later (recommended HAXM 7.2.0 or later)
35. 6
Android SDK
2. Android Emulator
In Android Virtual Emulator all functions that are
feasible on real Android mobile is works on virtual
Device like:
phone calls, text messages.
stimulate different network speeds.
specify the location of a device
access on google play store and lot’s more.
But there is one disadvantage of this emulator is
that.
It is very slow when System’s PC has less RAM.
It works fine when a maximum GB of RAM is
present on our device.
Android SDK Tools
36. 6
Android SDK
3. Android SDK Platform-tools
Android SDK Platform-tools is helpful when we are
working on Project and they will show the error
messages at the same time. It is specifically used
for testing. It includes:
a) Android Debug Bridge (ADB), is a command-
line tool that helps to communicate with the
device. It allows us to perform an action such
as Installing App and Debugging App etc.
b) Fastboot allows you to flash a device with a
new system image.
c) Systrace tools help to collect and inspect
timing information. It is very crucial for App
Debugging.
Android SDK Tools
37. 4) Android Virtual Devices (AVD)
An Android virtual device AVD is an emulator configuration that enables to model an actual
device by calling hardware and software options to be emulated by the Android Emulator.
the AVD manager can be used to create an AVD. launch it from eclipse by clicking Window| AVD
manager.
an Android virtual device AVD is used for testing the Android applications.
An AVD is an emulator occurrence that enables to form a real device Each AVD consists of a
hardware sketch, a connection to a system image, and emulated storage, such as a secure digital
SD card
38. 5) Android Virtual Devices (AVD)
An AVD consists of
A hardware profile: defines the hardware features of the virtual device. for example, you can
describe whether the device has a camera, whether it uses a physical QWERTY keyboard or a
dialling pad , how much memory has and so on.
A mapping to a system image: what version of the Android platform will run on the virtual
machine can be defined. The version of the standard Android platform or the system image
packaged with the SDK add on can be chosen
the emulator skin to be used with the AVD can be specified , which lets you control the screen
dimensions, look and so on .The emulated SD card to use with the AVD, also can be specified
A dedicated storage area on your development machine: the devices user area (installed
applications settings and so on ) and emulated SD card are stored in this area
Configuration of Android Environment
39. Application Lifecycle
Sr.
No.
DVM (Dalvik Virtual Machine) JVM (Java Virtual Machine)
1
It is Register based which is designed to run on low
memory.
It is Stack based.
2
DVM uses its own byte code and runs the “.Dex” file.
From Android 2.2 SDK Dalvik has got a Just in Time
compiler
JVM uses java byte code and runs “.class” file having JIT
(Just In Time).
3
DVM has been designed so that a device can run
multiple instances of the VM efficiently. Applications are
given their own instance.
A single instance of JVM is shared with multiple
applications.
4 DVM supports the Android operating system only. JVM supports multiple operating systems.
5 For DVM very few Re-tools are available For JVM many Re-tools are available.
6 There is a constant pool for every application. It has a constant pool for every class.
7 Here the executable is APK. Here the executable is JAR.
40. Application Lifecycle
• Application run in their own processes (VM, PID)
• Processes are started and stopped as needed to run an application's
components
• Processes may be killed to reclaim resources