Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Java Virtual Machine (JVM) Stack Area
Next article icon

Java Virtual Machine (JVM) Stack Area

Last Updated : 03 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The Java Virtual Machine is responsible for running Java applications, and it manages various memory areas, one of which is the Stack Area. In this article, we are going to discuss about JVM Stack Area in depth.

JVM Stack Area

In Java, each thread has its own stack called the Run-Time Stack, created when the thread starts. This stack holds the data for method execution within that thread. The memory for a Java Virtual Machine stack does not need to be contiguous. JVM implementations can allocate and manage stack memory dynamically, depending on the platform.

Components of a Stack Frame

The main components of a stack frame are listed below:

  • Method call information: Stores details about the method being executed.
  • Local variables: Stores local variables defined within the method.
  • Method parameters: Stores parameters passed to the method.
  • Return address: Tracks where to return once the method completes.

After all method calls are completed, the stack is emptied and destroyed by the JVM. The stack data is thread-specific, ensuring thread safety for local variables. Each entry in the stack is called a Stack Frame or Activation Record.

How the JVM Stack Works?

When a method is called, a new stack frame is created and pushed onto the thread's JVM stack, this stack frame contains all the information needed for the method execution. The JVM executes the method's code, using the local variables and parameters stored in the stack frame. Once the method execution is complete, the stack frame is popped off the stack, and control returns to the calling method.

Note: This process will repeat for every method call, including recursive and nested calls.

Example: Here, the example demonstrates how the stack actually works.

Java
// Java Program to Demonstrate the working of stack import java.io.*;  class Geeks {     public static int add(int a, int b) {         return a + b;     }     public static void main (String[] args) {         int ans = add(5, 5);         System.out.println("Answer: " + ans);     } } 

Output
Answer: 10 

Explanation:

  • When main method is called, the stack frame for the main is created.
  • Inside the main, the add method is called with the parameters, now the new stack frame for the add is created.
  • The add method executes using the provided parameters
  • After the add method returns, its stack frame is popped off and control returns to main.

The image below demonstrates the stack area of JVM:

Stack Area of JVM


Stack Frame Structure 

The stack frame basically consists of three parts. The three parts are

  • 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.

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)

  • The JVM uses an operand stack as a workspace for temporary calculations.
  • Unlike the local variable array, which you access using an index, the operand stack works with special instructions. These instructions can push values onto the stack, pop values off, and perform operations using those values.

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.

Next Article
Java Virtual Machine (JVM) Stack Area

A

anshul_aggarwal
Improve
Article Tags :
  • Java
  • java-basics
  • java-JVM
Practice Tags :
  • Java

Similar Reads

    How to Increase Heap Size in Java Virtual Machine?
    There are two types of memory stack memory and heap memory. All the dynamic allocations go into heap memory and the rest of the static allocations and variables allocations go into stack memory. Whenever a java program is executed in the java virtual machine it uses the heap memory to manage the dat
    3 min read
    Stack size() method in Java with Example
    The Java.util.Stack.size() method in Java is used to get the size of the Stack or the number of elements present in the Stack. Syntax: Stack.size() Parameters: The method does not take any parameter. Return Value: The method returns the size or the number of elements present in the Stack. Below prog
    2 min read
    Stack clear() method in Java with Example
    The Java.util.Stack.clear() method is used to remove all the elements from a Stack. Using the clear() method only clears all the element from the Stack and does not delete the Stack. In other words, we can say that the clear() method is used to only empty an existing Stack. Syntax: Stack.clear() Par
    2 min read
    Stack retainAll() method in Java with Example
    The retainAll() method of java.util.Stack class is used to retain from this stack all of its elements that are contained in the specified collection. Syntax: public boolean retainAll(Collection c) Parameters: This method takes collection c as a parameter containing elements to be retained from this
    3 min read
    Stack setSize() method in Java with Example
    The setSize() method of Java.util.Stack class changes the size of this Stack instance to the size passed as the parameter. Syntax: public void setSize(int size) Parameters: This method takes the new size as a parameter. Exception: This method throws ArrayIndexOutOfBoundsException if the new size is
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences