What is JVM Stack Area in Java

 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

  1. Java Virtual Machine creates a separate stack during the creation of a thread. 
  2. A stack associated currently in execution with a thread is called a runtime stack.  
  3. The Java Virtual Machine performs two operations—push and pop—upon the stack. 
  4. Push means to insert, and pop means to delete.
  5. , Once the thread completes executing, the related part from the stack is deleted.
  6. The stack is empty and deleted when all the calls by the thread are complete.
  7. Before terminating the thread, JVM destroys the stack respective to the thread.
  8. Only that thread is exclusively available to the stack that is associated with it, not other threads.
  9. It is known as a stack frame or activation record when every data in the stack is associated with the thread.
  10. 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: 

  1. Local variable array
  2. Frame data 
  3. 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. 

Conclusion

 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. 

References: 

  • https://tutorialspoint.dev/language/java/java-virtual-machine-jvm-stack-area
  • https://en.wikipedia.org/wiki/Java_virtual_machine

Leave a Reply

Your email address will not be published. Required fields are marked *