Java Virtual Machine: Aditya Gaur
Java Virtual Machine: Aditya Gaur
Aditya Gaur
Mentor: Vivek Beniwal
Java
• Created in 1991
• Network-oriented architecture
Java Architecture
• Java Programming Language
• Java API
Write Once, Run Anywhere
• Java is platform- independent
• Code compiled into Bytecode, which is interpreted by resident
JVM
• JVM is platform-dependent
Bytecodes
• Machine language for the JVM
• One stream of bytecode for each method in class
• Stored in method area of the JVM
• Sequence of instructions.
– One-byte opcode
– Followed by zero or more operands
Bytecode (cont…)
• Example 1
• Example 2
Bytecode(cont…)
• Why do we need to look at the byte code?
– Actually, the bytecode array contains bytes that represent the
instructions.
– Performance is a critical issue for many desktop and server systems that
use Java.
– Knowing what bytecode is generated for a sequence of Java instructions
can help you write smaller and more efficient code.
Bytecode(cont…)
• Example 3
• Example 4
JVM
• Clear mission in life: to run one Java application.
• The pc register indicates the next instruction to execute (the ones for threads one and
two) Because thread three is currently executing a native method, the contents of its
pc register is undefined.
Stack Frames
• formed as soon as a method is invoked.
• destroyed when method invocation completes (normal or abrupt)
• Each frame has
– array of local variables
– operand stack
– a reference to the runtime constant pool of the current method.
• Only one frame is active at a given time
• a frame created by a thread is local to that thread and cannot be
referenced by any other thread.
Stack Frames – Local Variables
• The local variables section contains a method's parameters and
local variables.
…
NOTE: Stack growing downward
Example
Local Variable Array Stack
static void justAdd();
Code: 0 100
0: bipush 100
2: istore_0 1
3: bipush 50
5: istore_1 2
6: iload_0
7: iload_1
8: iadd 3
9: istore_2
10: return 4
…
NOTE: Stack growing downward
Example
Local Variable Array Stack
static void justAdd();
Code: 0 100
0: bipush 100
2: istore_0 1
3: bipush 50
5: istore_1 2
6: iload_0
7: iload_1
8: iadd 3
9: istore_2
10: return 4
…
NOTE: Stack growing downward
Example
Local Variable Array Stack
static void justAdd();
Code: 0 100 50
0: bipush 100
2: istore_0 1
3: bipush 50
5: istore_1 2
6: iload_0
7: iload_1
8: iadd 3
9: istore_2
10: return 4
…
NOTE: Stack growing downward
Example
Local Variable Array Stack
static void justAdd();
Code: 0 100
0: bipush 100
2: istore_0 1 50
3: bipush 50
5: istore_1 2
6: iload_0
7: iload_1
8: iadd 3
9: istore_2
10: return 4
…
NOTE: Stack growing downward
Example
Local Variable Array Stack
static void justAdd();
Code: 0 100 100
0: bipush 100
2: istore_0 1 50
3: bipush 50
5: istore_1 2
6: iload_0
7: iload_1
8: iadd 3
9: istore_2
10: return 4
…
NOTE: Stack growing downward
Example
Local Variable Array Stack
static void justAdd();
Code: 0 100 100
0: bipush 100
2: istore_0 1 50 50
3: bipush 50
5: istore_1 2
6: iload_0
7: iload_1
8: iadd 3
9: istore_2
10: return 4
…
NOTE: Stack growing downward
Example
Local Variable Array Stack
static void justAdd();
Code: 0 100 150
0: bipush 100
2: istore_0 1 50
3: bipush 50
5: istore_1 2
6: iload_0
7: iload_1
8: iadd 3
9: istore_2
10: return 4
…
NOTE: Stack growing downward
Example
Local Variable Array Stack
static void justAdd();
Code: 0 100
0: bipush 100
2: istore_0 1 50
3: bipush 50
5: istore_1 2 150
6: iload_0
7: iload_1
8: iadd 3
9: istore_2
10: return 4
…
NOTE: Stack growing downward
Java Stack – run time constant pool
• Each frame contains a reference to the runtime constant pool
• A runtime constant pool is a per-class or per-interface runtime
representation of the constant_pool table in a class file.
• It contains several kinds of constants, ranging from numeric
literals known at compile time to method and field references that
must be resolved at run time.
• The runtime constant pool serves a function similar to that of a
symbol table for a conventional programming language, although
it contains a wider range of data than a typical symbol table.
The Class Loader Subsystem
• The part of a Java virtual machine implementation that takes care
of finding and loading types is the class loader subsystem
• the Java virtual machine contains two kinds of class loaders:
– a bootstrap class loader
– user-defined class loaders.
• the bootstrap class loader is a part of the virtual machine
implementation.
• Classes loaded by different class loaders are placed into
separate name spaces inside the Java virtual machine.
The Execution Engine
• In Java Specification the behavior of the EE is defined in terms of
the Instruction set.