This thread is used to store partial results, method returns, and invocation.
How JVM works?
JVM performs as a run time engine to run Java applications. JVM is a part of the Java Runtime Environment.
JVM enables Java applications to develop Java code on one system and can run on any other Java-enabled system.
When you compile class files, the Java compiler compiles a .java file with the same class names available in the .java file.
This .class file undergoes different steps when you run it. All these steps explain the entire JVM.
Run – Time Stack
At the time of thread creation, JVM creates a separate stack. And while performing two operations on Java Stacks, It pushes and pops frames. A stack related to a specific thread can be called Run – Time Stack.
The Run -Time Sack stores every method call done by the thread, assignment of a local variable, intermediate calculations, and calling parameters as an operation.
Then, every method performed by that thread is stored in local variables, intermediate computations, and a corresponding Run – Time Stack.
When the method is complete, a corresponding entry is removed from the stack.
The stack becomes empty after completing all method calls, and then the JVM destroys the empty stack before terminating the thread.
The data in the stack is not accessible to the remaining threads but is available for the corresponding thread.
Hence every entry you made in the stack is called Activation Record or Stack Frame.
Main points to remember about JVM Stack Area
- Java Virtual Machine creates a separate stack during the creation of a thread.
- A stack associated currently in execution with a thread is called a runtime stack.
- The Java Virtual Machine performs two operations—push and pop—upon the stack.
- Push means to insert, and pop means to delete.
- , Once the thread completes executing, the related part from the stack is deleted.
- The stack is empty and deleted when all the calls by the thread are complete.
- Before terminating the thread, JVM destroys the stack respective to the thread.
- Only that thread is exclusively available to the stack that is associated with it, not other threads.
- It is known as a stack frame or activation record when every data in the stack is associated with the thread.
- For the JVM stack, it is not essential to have contiguous memory locations.
The structure of Stack Frame
It has three constituents that run as follows:
- Local variable array
- Frame data
- Operand stack
A variable array has a name and an index, which enables it to choose an element. It is stored under different values but with the same name.
As for Frame data, it contains method returned data and data value.
The Operand Stack works like a workspace to the Java Virtual Machine, which is employed to store intermediate computational results.
It is structured like a variable array, locally accessible by doing pop and push operations on the stack.
In short, the two main functions of JVM are to allow Java programs to run on any device and to optimize and manage program memory.