Open In App

Java Virtual Machine (JVM) Stack Area

Last Updated : 20 Jun, 2021
Like Article

For every thread, JVM creates a separate stack at the time of thread creation. The memory for a Java Virtual Machine stack does not need to be contiguous. The Java virtual machine only performs two operations directly on Java stacks: it pushes and pops frames. And stack for a particular thread may be termed as Run – Time Stack. Every method call performed by that thread is stored in the corresponding run-time stack including parameters, local variables, intermediate computations, and other data. After completing a method, the corresponding entry from the stack is removed. After completing all method calls the stack becomes empty and that empty stack is destroyed by the JVM just before terminating the thread. The data stored in the stack is available for the corresponding thread and not available to the remaining threads. Hence we can say local data thread-safe. Each entry in the stack is called Stack Frame or Activation Record



Stack Frame Structure 
The stack frame basically consists of three parts: Local Variable Array, Operand Stack & Frame Data. When JVM invokes a Java method, first it checks the class data to determine the number of words (size of the local variable array and operand stack, which is measured in words for each individual method) required by the method in the local variables array and operand stack. It creates a stack frame of the proper size for invoked method and pushes it onto the Java stack. 

1. Local Variable Array (LVA): 

  • The local variables part of the stack frame is organized as a zero-based array of words.
  • It contains all parameters and local variables of the method.
  • Each slot or entry in the array is of 4 Bytes.
  • Values of type int, float, and reference occupy 1 entry or slot in the array i.e. 4 bytes.
  • Values of double and long occupy 2 consecutive entries in the array i.e. 8 bytes total.
  • Byte, short, and char values will be converted to int type before storing and occupy 1 slot i.e. 4 Bytes.
  • But the way of storing Boolean values is varied from JVM to JVM. But most of the JVM gives 1 slot for Boolean values in the local variable array.
  • The parameters are placed into the local variable array first, in the order in which they are declared.
  • For Example: Let us consider a class Example having a method bike() then the local variable array will be as shown in the below diagram:


// Class Declaration
class Example
  public void bike(int i, long l, float f, 
               double d, Object o, byte b)


Local Variable Array for bike()

2. Operand Stack (OS): 

  • JVM uses operand stack as workspace like rough work or we can say for storing intermediate calculation’s result.
  • The operand stack is organized as an array of words like a local variable array. But this is not accessed by using an index like local variable array rather it is accessed by some instructions that can push the value to the operand stack and some instructions that can pop values from the operand stack and some instructions that can perform required operations.
  • For Example: Here is how a JVM will use this below code that would subtract two local variables that contain two ints and store the int result in a third local variable:


Assembly Code Instruction for Operand Stack


  • So here first two instructions iload_0 and iload_1 will push the values in the operand stack from a local variable array. And instruction isub will subtract these two values and store the result back to the operand stack and after istore_2 the result will pop out from the operand stack and will store into a local variable array at position 2.


Working of LVA and OS

3. Frame Data (FD): 

  • It contains all symbolic references (constant pool resolution) and normal method returns related to that particular method.
  • It also contains a reference to the Exception table which provides the corresponding catch block information in the case of exceptions.


Previous Article
Next Article

Similar Reads

How JVM Works - JVM Architecture?
JVM(Java Virtual Machine) acts as a run-time engine to run Java applications. JVM is the one that actually calls the main method present in a Java code. JVM is a part of JRE(Java Runtime Environment). Java applications are called WORA (Write Once Run Anywhere). This means a programmer can develop Java code on one system and can expect it to run on
7 min read
Verification in Java (JVM)
After the class loader in the JVM loads the byte code of .class file to the machine the Bytecode is first checked for validity by the verifier and this process is called as verification. The verifier performs as much checking as possible at the Linking so that expensive operation performed by the interpreter at the run time can be eliminated. It en
3 min read
Types of JVM Garbage Collectors in Java with implementation details
prerequisites: Garbage Collection, Mark and Sweep algorithm Garbage Collection: Garbage collection aka GC is one of the most important features of Java. Garbage collection is the mechanism used in Java to de-allocate unused memory, which is nothing but clear the space consumed by unused objects. To deallocate unused memory, Garbage collector track
5 min read
Difference between JIT and JVM in Java
Java Virtual Machine (JVM) is used in the java runtime environment(JRE). The original JVM was conceived as a bytecode interpreter. This may come as a bit of a surprise because of performance problems. Many modern languages are meant to be compiled into CPU-specific, executable code. The fact that the JVM executes a Java program, however, helps addr
4 min read
Java Program to Check if JVM is 32 or 64 bit
JVM Stands for Java Virtual Machine. It is an abstract computing machine that allows a computer to run a Java program. It is a platform-independent environment, responsible for compiling Java programs by performing conversion of Java code into machine language i.e. byte code. The memory needed by JVM to run a Java program is called heap memory. In
2 min read
Difference Between 32-bit and 64-bit JVM in Java
One does not need to know the difference unless developing a performance-critical application. The minor distinction between 32-bit and 64-bit JVMs would make little difference in your application. Now coming onto the differences where we will be listing out some key differences between 32-bit and 64-bit Java Virtual Machines are listed below in wh
4 min read
Different Ways to Collect Garbage in Java HotSpot JVM
JDKs come with different JVM implementations (Oracle HotSpot, Eclipse OpenJ9, GraalVM, Jikes RVM, Codename One), and different garbage collector implementations with different tuning options (Serial, Parallel, CMS, G1, Z, Shenandoah). In this article, we will know more about the Garbage Collector, how it works, and the various types of GC available
7 min read
JVM Shutdown Hook in Java
Shutdown Hooks are a special construct that allows developers to plug in a piece of code to be executed when the JVM is shutting down. This comes in handy in cases where we need to do special clean up operations in case the VM is shutting down.Handling this using the general constructs such as making sure that we call a special procedure before the
6 min read
How to Generate JVM Heap Memory Dump?
Java Heap dump is a snapshot of all java objects that are present in the JVM(Java Virtual Machine) at a certain point in time. The JVM allocates memory for objects which are class instances or arrays in the heap memory. When the objects are no longer needed or are no more referenced, the Garbage Collector runs and reclaims the memory space occupied
7 min read
Difference Between JVM and DVM
JVM is the virtual machine that runs java code on different platforms. It acts as an abstract layer between the program and the platform on which the java code is running. The portability of Java code is possible only because of the JVM. The javac compiler converts the source code file(.java file) into an intermediate java bytecode format which is
3 min read
Article Tags :
Practice Tags :