0% found this document useful (0 votes)
52 views

Java Virtual Machine

The document discusses Java Virtual Machines (JVMs) and how they execute Java programs. It covers: 1) Java code is compiled to bytecode that can run on any JVM regardless of device or operating system. The JVM handles running the bytecode and includes components like the class loader, execution engine, and memory areas. 2) There are different JVMs for different devices - from resource-intensive desktops to resource-constrained devices. The Java ME specification defines configurations and profiles to support different devices. 3) Key aspects of a JVM include class loading, verification, memory layout with the heap and method area, execution via frames on the stack, and optimized just-in-
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Java Virtual Machine

The document discusses Java Virtual Machines (JVMs) and how they execute Java programs. It covers: 1) Java code is compiled to bytecode that can run on any JVM regardless of device or operating system. The JVM handles running the bytecode and includes components like the class loader, execution engine, and memory areas. 2) There are different JVMs for different devices - from resource-intensive desktops to resource-constrained devices. The Java ME specification defines configurations and profiles to support different devices. 3) Key aspects of a JVM include class loading, verification, memory layout with the heap and method area, execution via frames on the stack, and optimized just-in-
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 86

Java Virtual Machine

Java: Not a Traditional Programming Language

Java Java Byte


Source Java Compiler code (*.cl
(*.java) ass)

Java bytecode
(moves locally or via web)

Java Platform
Linux Win32/NT Solaris
Hardware
Java Platform
Java Platform & VM & Devices
Java 2 XXX Edition
 Each edition is a developer treasure chest of tool
s and supplies that can be used with a particular
product:
 Java virtual machines that fit inside the range of cons
umer devices
 a library of APIs that are specialized for each type of
device
 tools for deployment and device configuration
 a profile, that is, a specification of the minimum set o
f APIs useful for a particular kind of consumer device
(set-top, screenphone, wireless, car, and digital assist
ant) and a specification of the Java virtual machine fu
nctions required to support those APIs
Java 2 Standard Edition
Java 2 Enterprise Edition
Java 2 Micro Edition
Various VM
 JVM (J2EE & J2SE)
 Well-known Java Virtual Machine.
 CVM, KVM (J2ME)
 Addresses on vast small devices.
 Reduces some VM features to fit resource-
constrained devices.
 JCVM (Java Card)
 Addresses on smart cards.
 It has least VM features.
J2ME : Configuration
 J2ME technology has two design centers--
things that you hold in your hand and
things you plug into a wall.

 Configurations are composed of the two


low-level APIs and optimized virtual
machines .
J2ME : Profile
 A profile is a specification that details the
Java technology APIs, built on top of and
utilizing the underlying Configuration.

 Profiles specify both APIs and the


Configuration.
Profiles:Foundation & MIDP
 MIDP:Mobile Information Device Profile
 Foundation is a set of JavaTM APIs which,
together with the Connected Device Confi
guration (CDC).
 MIDP is a set of Java APIs which, together
with the Connected Limited Device Config
uration (CLDC).
CVM & CDC (1)
 CDC : Connected Device Configuration
 CVM is a Java 2 virtual machine designed for
devices needing the functionality of the Java 2
virtual machine feature set, but with a smaller
footprint.
 The Connected Device Configuration (CDC)
includes the CVM and basic class libraries to
support Java language applications on consumer
electronic and embedded devices.
CVM & CDC (2)
 CDC is designed for the devices such as s
mart communicators, pagers, personal digi
tal assistants (PDAs), and interactive, digit
al television set-top boxes.

 Typically, these devices run a 32-bit micro


processor/controller and have more than 2
.0MB of total memory.
KVM & CLDC (1)
 CLDC : Connected Limited Device Configuration
 K virtual machine (KVM) is a new, highly optimiz
ed Java virtual machine specifically designed for
resource-constrained consumer devices.
 CLDC outlines the most basic set of libraries and
JavaTM virtual machine features that must be pr
esent in each implementation of a Java 2 Platfor
m, Micro Edition environment on highly constrain
ed devices.
KVM & CLDC (2)
 CLDC is designed for small, resource-constrained
devices, such as mobile phones, mainstream
personal digital assistants, and small retail
payment terminals.

 CLDC is suitable for devices with 16/32-bit


RISC/CISC microprocessors/controllers, and with
as little as 160 KB of total memory available.
JCVM (1)
JCVM (2)
JCVM (3)
 JCVM has least VM features defined in
JVM SPEC.
 Unsupported Features:
 Dynamic Class Loading
 Security Manager
 Garbage Collection & Finalization
 Threads
 Cloning
JCVM (4)
 Unsupported Keywords:
 native
 synchronized
 transient
 volatile
 Unsupported Types:
 char, float, double, long
 Arrays that is more than one dimension.
JCVM (5)
 In general, none of Java core API classes
are supported in the Java Card platform.
 Some classes from java.lang package are
supported, but none of the rest are.
 Class java.lang.System is not supported. I
nstead, it supplies a class javacard.framew
ork.JCSystem, which provides an interface
to system behavior.
VM Mechanism
Internal Architecture of JVM
Java Runtime System

Java bytecode
class m Exception,
(*.class)
ethodar Heap Thread,
Class Loader ea
& Verifier Security.

Java Stack,
Java Class
Register
Library

native
native execution engine
native method method
method linker
area

Operating System
Method Area & Heap
 Each JVM implementation has a heap and
a method area.
 Heap is shared among all threads. It is the
runtime data area from which for all class
instances and arrays is allocated.
 Method area is shared among all threads.
It stores per-class structures such as the
runtime constant pool, field and method
data, the code for methods.
Class Loader & Verifier
 Loading: Load binary representation of class (*.c
lass).
 Linking:
 Verification: Ensure that the binary representation is s
tructurally valid.
 Preparation: Create the static fields for the class or int
erface and initializing those fields to their standard de
fault values.
 Resolution: Determine concrete values from symbolic
references in the runtime constant pool .
 Initializing: Invoke its static initializers and the in
itializers for static fields declared in class.
Register
 PC (program counter) register. It contains
the address of the Java virtual machine
instruction currently being executed.

 Each thread has its own PC register.

 If the method currently being executed by


the thread is native, the value of the Java
virtual machine's pc register is undefined.
Java Stack
 Java Stack stores frames.
 Each thread has a private JVM stack,
created at the same time as thread.
 Java Stack holds local variables and partial
results, and plays a part in method
invocation and return.
Java Stack : frame (1)
 A new frame is created each time a
method is invoked. A frame is destroyed
when its method invocation completes.
 A frame is used to store data and partial
results, as well as to perform dynamic
linking, return values for methods, and
dispatch exception.
Java Stack : frame (2)
 Each frame has its own array of local
variables, its own operand stack, and a
reference to the runtime constant pool of
the class of the current method.
local variables

operand stack

others…
frame
Java Stack
Java Stack : frame (3)
class LocalVariableExample {
public static void runClassMethod(int i, long l, float f) { }
public void runInstanceMethod(char c, double d, boolean b) {
}
}

runClassMethod() runInstanceMethod
()
0 int i 0 reference
1 char c
1 long l
2 double d
3 float f
4 boolean b
Java Stack : frame (4)
iload_0
iload_1
iadd
istore_2

local variables

0 100 0 100 0 100 0 100 0 100


1 98 1 98 1 98 1 98 1 98
2 2 2 2 2 198

100 100 198


98

operand stack
Java Stack : frame (5)
 class pointer
 previous frame pointer
 exception table
 ……
Execution Engine (1)
 A virtual processor -- the core of JVM.
 Programming aspect : interpreter.
 Read bytecode stream then execute the in
structions.
 The performance can be improved by Jus
t-In-Time(JIT) compiler.
Execution Engine (2)
iload 1

int y=x+5; bipush 5


iadd
istore 2

iload 1 bipush 5 iadd istore 2


21 1 16 5 96 54 2
Java
Assembly Language
Count.java
public class Count
{
public static void main(String[] args)
{
int i;
for(i=0;i<10;i++)
{
System.out.println(java.lang.String.valueof(i));
}
}
}
Count.j (1)
.class public Count
.super java/lang/Object

.method public <init>()V


aload_0
invokespecial java/lang/Object/<init>()V
return
.end method
Count.j (2)

.method public static main([Ljava/lang/String;)V


.limit stack 3
.limit locals 4

getstatic java/lang/System/out Ljava/io/PrintStream;


astore_1
bipush 10
istore_2
Count.j (3)
Loop:
bipush 10
iload_2
isub
invokestatic java/lang/String/valueOf(I)Ljava/lang/String;
astore_3
aload_1
aload_3
invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V

iinc 2 -1
iload_2
ifne Loop

return
.end method
Java Assembler
 Not an official assembler. There is no stan
dard for java assembly language.

 http://mrl.nyu.edu/~meyer/jasmin/
Class File Structure
Tables of Class File
 Top Table:
 Basic information of the whole class.
 Constant Pool Table:
 Records string, constant, reference, method name, field name…
 Interface Table:
 What interface are implemented by this class.
 Field Table:
 The fields which class owns.
 Method Table:
 The methods which class owns.
 Attribute Table:
 The extra information of class, field, method… and so on.
Magic Number
 0xCA, 0xFE, 0xBA, 0xBE

 Every class file contains these 4-byte data


in the header.
Type Descriptor (1)
 Primitive Type:
 B : byte
 C : char
 D : double
 F : float
 I : int
 J : long
 S : short
 Z : boolean
Type Descriptor (2)
 Class Type:
 L<fullclassname>;

 Ex1: Ljava/lang/String;
 Ex2: Ljava/io/PrintStream;
Type Descriptor (3)
 Array Type:
 [<type>

 Ex1: [C  char[]
 Ex2: [[F  float[][]
 Ex3: [Ljava/lang/Thread;  Thread[]
Type Descriptor (4)
 Method:
 (<argument_types>)<return_type>

 “V” indicates “void” (used in return type)

 Ex1: ()V
 void xyz() {…}
 Ex2: (SF[Ljava/lang/Thread;)I
 int xyz(short x, float y, Thread[] z) {…}
Constant Pool
Tag Value Description
CONSTANT_Utf8 1 array of byte
CONSTANT_Integer 3 integer
CONSTANT_Float 4 float
CONSTANT_Long 5 long
CONSTANT_Double 6 Double
CONSTANT_Class 7 Tag-1 index
CONSTANT_String 8 Tag-1 index
CONSTANT_Fieldref 9 Tag-7 index + Tag-12 index
CONSTANT_Methodref 10 Tag-7 index + Tag-12 index
CONSTANT_InterfaceMethodref 11 Tag-7 index + Tag-12 index
CONSTANT_NameAndType 12 Tag-1 index(filed,method name)
+ Tag-1 index(type descriptor)
Access Flag
Flag Value
ACC_PUBLIC 0x0001
ACC_PRIVATE 0x0002
ACC_PROTECTED 0x0004
ACC_STATIC 0x0008
ACC_FINAL 0x0010
ACC_SUPER 0x0020
ACC_SYNCHRONIZED 0x0020
ACC_VOLATILE 0x0040
ACC_TRANSIENT 0x0080
ACC_NATIVE 0x0100
ACC_INTERFACE 0x0200
ACC_ABSTRACT 0x0400
out.j
.class public out
.super java/lang/Object

.method public <init>()V


aload_0
invokespecial java/lang/Object/<init>()V
return
.end method

.method public static main([Ljava/lang/String;)V


.limit stack 2

getstatic java/lang/System/out Ljava/io/PrintStream;


ldc “Hello World”
invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V

return
.end method
out.class
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
public + super
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
self class name
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
super class name
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
interface number
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
field number
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
method number
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
public
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
method name
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
method type
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
attribute number
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
attribute type
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
method length
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
[
]
stack size
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
[
]
local variable size
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
[
]
bytecode length
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
[
]
bytecode content
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
[
]
exception size
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
[
]
extra attribute number
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
[
]
public + static
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
method name
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
method type
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
attribute number
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
attribute type
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
method length
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]

[
]
stack size
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]

[
]
local variable size
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]

[
]
bytecode length
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]

[
]
bytecode content
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]

[
]
exception size
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]

[
]
extra attribute number
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]

[
]
atttribute number
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
attribute type
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
attribute length
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
attribute data
[ ][ ][ ] [1 ][
2

] [3 4
5 6
][ 7 8
][ 9
][ 10
][ ] [11
][ 12
][ 13
][
][ 14
][
15 16
][ ]
[ ][ 17
18 19
][
][ ][
21
] [20
22 23 24
][ 25
]
[ ][ ][ ][
26
][
]
The End

You might also like