'JVM Stack Variables
I understand JVM creates a Stack for each thread and that Stack contains calls to other methods as thread invokes them. I don't understand when it says Stack will also have local variables and partial results. I thought global and local variables (primitives and references) would exist in heap rather than in Stack, can anybody please elaborate what it does it mean? Secondly it says partial results, does it mean when thread switching happens those half executed results (copies) from local and instance variables?
Thanks
-Abidi
Solution 1:[1]
Each JVM has a runtime stack of method invocation frames. Each method frame contains
- A reference to the Java class containing that method.
- An operand stack for holding temporary values.
- A "local variables" array for holding function arguments and temporary results.
This local variables array exists so that when the function is first called, the arguments to that function can be stored somewhere. The local variables array does not actually hold all the local variables declared in the Java source code; rather, it's more of a temporary buffer for holding references to Java objects declared elsewhere in the heap, or to hold values that are referenced enough times that putting them on the runtime stack would be slow or inefficient.
In short, you are correct that locals and globals are stored in the heap. The "local variables" array in Java threads doesn’t correspond to these locals, but rather to scratch space used by the the thread while interpreting the bytecode for the method.
Solution 2:[2]
Each JVM has a runtime stack of method invocation frames. Each method frame contains
A reference to the Java class containing that method.
An operand stack for holding temporary values.
A "local variables" array for holding function arguments and temporary results.
This local variables array exists so that when the function is first called, the arguments to that function can be stored somewhere. The local variables array does not actually hold all the local variables declared in the Java source code; rather, it's more of a temporary buffer for holding references to Java objects declared elsewhere in the heap, or to hold values that are referenced enough times that putting them on the runtime stack would be slow or inefficient.
In short, you are correct that locals and globals are stored in the heap. The "local variables" array in Java threads don't correspond to these locals, but rather to scratch space used by the the thread while interpreting the bytecode for the method.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Harshad Nasit |