JVM Stack and Frame

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

JVM the execution of a Java program to load all the data, such as the type of information (Class), an instance of a type (Instance), constant data (Constant), local variables, etc. Different data stored in different memory area, the data memory area called the "runtime data area(Runtime Data Area)". The runtime data areas are important areas such several: JVM Stack (referred to as Stack or virtual machine stack, thread stack, stack), Frame (also called StackFrame/ stack frame, method of stack), Heap (/GC heap object heap, garbage collection area). Following a brief introduction of Stack and Frame, for Heap, please refer to the garbage collection articles.


Sharing a single thread within the district: PC Register/JVM Stack/Native Method Stack.
All thread shared area: Heap/Method Area/Runtime Constant Pool.

Above: the runtime data areas. The focus is on sharing each thread has PCRegister/Stack and thread Heap and constant pool(ConstantPool)

Above: the thread stack (VM Statck/Stack) stack frame contains (Frame). The focus is the stack frame and its structure, operation stack (OperandStack) and constant pool references.


Structure: {JVM Stack [Frame][Frame][Frame]... }.
By creating JVM Stack was created in each thread, used to store a set of stack frame(StackFrame/Frame).
JVM Statck in size can be fixed, can also be a dynamic extension. If a thread to a fixed size larger than Stack, StackOverflowError will occur; if no dynamic extended Stack enough memory or system does not have enough memory for the creation of a new thread Stack, OutOfMemoryError.


Structure: {Frame [ReturnValue] [LocalVariables[][][][]...] [OperandStack [][][]...] [ConstPoolRef] }
Each method invocation will create a corresponding Frame, method is executed or abnormal termination, Frame is destroyed. A method of A calls another method B, A frame, a new frame is created with B, after the execution is completed, the calculated results are passed to A, A to continue.

The local variable table
The local variable table size is determined at compile time. Primitive types and reference data and the return address (returnAddress) occupy a local variable size, long/double requires two.

Java code "int a=0; int b=1; int c=2;" corresponding to the local variable table as follows:

Start Length Slot Name Signature
2 12 0 a I
4 10 1 b I
6 8 2 c I

Start: variable offset.
Length: scope of length. [Start, Start+Length) is the scope of the variable.
Slot: a Slot can store 32bit data types, reference, return address, long/dobule requires two Slot.

Operation stack(OperandStack)
The Frame is created, operation of the stack is empty. Each item can be stored JVM data of various types of operation stack, including the long/double.
Operation stack is a stack of deep, long/double contributed two stack depth.
Operation stack call other methods with return results, the results of push onto the stack.

Java code:

int a=1;
int b=2;
int c=a+b;

The corresponding instruction:

0: iconst_1 // Push 1 to the operation of the stack. A value of int greater than 5 will use the bipush <i> instruction. 
1: istore_0 // Pop the top element, stored in the index=0 local variable. 
2: iconst_2 // Push 2 to the operation of the stack
3: istore_1 // Pop the top element on the stack, stored in the index=1 local variable. 
4: iload_0  // The local variable load index=0 to the top of the stack
5: iload_1  // The local variable load index=1 to the top of the stack
6: iadd     // The top two numbers pop out together, and stores the result to the top of the stack
7: istore_2 // Store the results to the index=2 local variable


2. http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html#jvms-2.5
3. Understanding the Java virtual machine
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Ward at October 22, 2014 - 4:24 PM