So I know that there exists 2 memory areas: Stack and Heap.
I also know that if you create a local variable it will live in the Stack, not in the heap. Stack will grow as we push data into it as in:
Now I will try to pass the confusion I am having to you:
For example this simple Java Code:
public class TestClass {
public static void main(String[] args) {
Object foo = null;
Object bar = null;
}
}
is translated into this byte code:
public static void main(java.lang.String[]);
Code:
Stack=1, Locals=3, Args_size=1
0: aconst_null
1: astore_1
2: aconst_null
3: astore_2
4: return
LineNumberTable:
line 5: 0
line 6: 2
line 7: 4
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 args [Ljava/lang/String;
2 3 1 foo Ljava/lang/Object;
4 1 2 bar Ljava/lang/Object;
where by definition acons_null is:
push a null reference onto the stack
and astore_1 is:
store a reference into local variable 1
The confusion I am having is, we pushed the foo into stack, then we stored it in the stack again? What is meant by storing a reference in a local variable? Where does that local variable live? The same stack we pushed the foo into or are these seperate Stacks?
Now at that point, if I call a method on the first object I pushed into the stack, since the stack pointer is pointing to the last element I pushed, how will it be processed?
After the function returns, the stack memory of this function is deallocated, which means all local variables become invalid. The allocation and deallocation for stack memory is automatically done. The variables allocated on the stack are called stack variables, or automatic variables.
The stack is used for dynamic memory allocation, and local variables are stored at the top of the stack in a stack frame. A frame pointer is used to refer to local variables in the stack frame. Figure 110: Stack frame before and after the LINK instruction.
EBP—Stack Data Pointer Register is used to reference functions and stack variables in the current stack frame. The EBP is also known as the “Frame Pointer.”
The size of memory to be allocated is known to the compiler and whenever a function is called, its variables get memory allocated on the stack.
There exists one stack per thread in the JVM. Each stack is composed of several frames: each method invocation creates a new frame, and when the method invocation is done, the frame is destroyed.
Within a stack frame there are two areas :
Depending on the JVM implementation, they may or may not be contiguous in memory. Logically they are two separate sections of the stack frame.
As explained in the description of aconst_null
, the aconst_null
instruction pushes the null
object reference onto the operand stack.
And as explained in the description of astore_<n>
(where n
could be 0, 1, 2 or 3):
The
<n>
must be an index into the local variable array of the current frame (§2.6). Theobjectref
on the top of the operand stack must be of typereturnAddress
or of typereference
. It is popped from the operand stack, and the value of the local variable at<n>
is set toobjectref
.
So in your example, the statement Object foo = null
translates to the following:
null
(a special reference that points to "nothing") onto the top of the operand stack.operand stack __________ | null | <-- null is pushed on the operand stack |__________| | | |__________| | | |__________|
foo
.operand stack local variables __________ _______________ _______________ _______________ _______________ | | | args | foo (null) | | | |__________| |_______0_______|_______1_______|_______2_______|_______3_______| | | store null in LV#1 |__________| | | |__________|
Same steps are done for Object bar = null
except that null
is stored in the local variable at index 2.
Source: Java Virtual Machine Specification (See this section).
You should look at structure of Java stack frame.
A java stack frame contains 3 things :
- A local variable table
- An operand stack
- A reference to class's constant pool AKA Frame Data
So, push a null reference onto the stack
--> pushes the reference onto the operand stack.
store a reference into local variable 1
--> stores the reference into slot 1 of local variable table
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With