Java Virtual Machine
Aditya Gaur
Mentor: Vivek Beniwal
Java
• Created in 1991
• By James Goslings, Sun Microsystems
• Original name was Oak
• Network-oriented architecture
Java Architecture
• Java Programming Language
• Java Virtual Machine (JVM)
• 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.
• an imaginary machine that is implemented by emulating software
on a real machine
• ‘n’ Java applications running – ‘n’ instances of JVM
JVM (architecture)
Run Time Data Areas
• When JVM runs a program it needs memory to store many things:
– Bytecodes
– objects the program instantiates
– parameters to methods
– return values
– local variables
– intermediate results of computations.
• The specification is quite abstract.
• Details are left for individual implementations.
• But some data areas are common.
Runtime Data Areas – Method Area
• Shared by all threads running inside the JVM.
• analogous to the "text" segment (compiled code) in a UNIX
process.
• It stores
– the runtime constant pool
– field and method data
– the code for methods and constructors
Runtime Data Areas – Heap
• Shared by all threads running inside the JVM
• All objects and array instantiated are kept into the heap.
• Storage is reclaimed by Garbage Collector
• JVM implementation may provide the programmer control over
the size of heap.
Runtime Data Areas – PC Registers
• Each thread has its own PC register.
• The value of PC register indicates the next instruction to execute.
• The pc register is one word in size
• If a thread is executing a native method, the value of the pc
register is undefined.
Runtime Data Areas – Native method
stacks
• A JVM implementation may use conventional stacks, informally
called "C stacks," to support native methods
• When a call to native method is made then, no entry in Java Stack
• The implementation that do not support native methods need not
supply native method stacks.
• Typically created per thread.
Runtime Data Areas – Java Stacks
• allocated per thread.
• stores Frames.
• memory may not be contiguous
• size can be controlled by the programmer
Runtime Data Areas – Java Stacks
• 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.
• Addressed by indexing – Zero based
• The different types use different amount of space:
– 1 Unit for int, float, reference, returnAddress
– 1 Unit for byte, short, char (converted to int before being stored)
– 2 Units for long and double (consecutive)
Stack Frames – Local Variables
class Example {
public static int runClassMethod (int i, long l, float f, double d, Object o, byte b) {
return 0;
}
public int runInstanceMethod (char c, double d, short s, boolean b) {
return 0;
}
}
Stack Frames – Local Variables
• NOTE: Apart from the method’s parameter, Java compiler can
arrange the local variables in any order, depending on their
implementation.
• They can also use the same index for two different variables but
with non-overlapping scope as in:
for (int i=0 ; i<10; i++){
syso(i);
}
for (int j=10; j>0; j--){
syso(j);
}
Stack Frames - Operand Stack
• Organization of data is in the similar way as in local variable
array.
• Access is through push and pop.
• Apart from the PC, the JVM has no registers.
• JVM is stack based rather than register based.
Example
Local Variable Array Stack
static void justAdd();
Code: 0
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
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.
• What should be done when a particular instruction is encountered.
• But it does not specify how.
• Implementation is left up for the designers.
Thank You