1. Home
Java

Step by Step Java Tutorial Concepts - From Beginner to Pro

Learn Java programming from basics to advanced concepts with our comprehensive tutorials. Start from scratch and elevate your skills to expert level.

  • 192 Lessons
  • 32 Hours
right-top-arrow

Tutorial Playlist

191 Lessons
72

Heap Memory and Stack Memory in Java

Updated on 11/03/20245,932 Views

Introduction

Memory in Java can be categorized into two separate sections; heap memory and stack memory. The stack memory is where objects are dispensed and deallocated dynamically while executing a program.

It is overseen by the Java Virtual Machine (JVM), leaving a huge and adaptable storage space for objects. Stack memory, however, stores local variables and method calls. It is important to learn about heap memory and stack memory in Java with examples, their use cases, and their differences in managing memory and preventing memory leaks and stack overflows efficiently.

Overview

This tutorial is a detailed guide to understanding what is heap memory and stack memory in Java. You will gain an in-depth understanding of the same with examples illustrating the use cases of both these categories, their advantages, and their disadvantages.

What is Stack Memory in Java?

Stack memory in Java refers to a part of the memory used to store method calls and local variables. It operates in a LIFO (last-in-first-out) manner, where the items added most recently are accessed first. It is a limited and efficient memory space automatically managed by the JVM, making it suitable for handling method invocations and storing temporary data during program execution.

Example of Stack Memory in Java

Here is an example of stack memory usage in Java:

public class upGrad {
    public static void main(String[] args) {
        int a = 5;
        int b = 10;

        int sum = calculateSum(a, b);
        System.out.println("Sum: " + sum);
    }

    public static int calculateSum(int x, int y) {
        int result = x + y;
        return result;
    }
}

In this example, the stack memory is used to store the method frames and local variables for the main method and the calculateSum method. As each method is called, a new stack frame is created, and when a method finishes its execution, its stack frame is removed from the stack. This allows for efficient memory management and helps in organizing the execution of methods and handling local variables.

The variables a and b are declared and initialized with values 5 and 10, respectively. These variables are stored in the stack frame of the main method. The calculateSum method is called, passing the values of a and b as arguments. A new stack frame is created for the calculateSum method.

Inside the calculateSum method, the parameters x and y are declared and assigned the values passed from the main method. These variables are stored in the stack frame of the calculateSum method. The variable result is declared and assigned the sum of x and y. This variable is also stored in the stack frame of the calculateSum method.

The result variable is returned to the main method. The stack frame of the calculateSum method is removed from the stack. The value returned from the calculateSum method is stored in the variable sum in the stack frame of the main method. The value of sum is printed to the console.

What is StackOverflowError in Java?

In Java, a StackOverflowError exception occurs when the stack memory, which is used to track method calls and store local variables, exceeds its limit. This typically happens when there is a recursive method call without a proper base case, causing an infinite loop and exhausting the available stack space.

When a StackOverflowError occurs, it indicates that the program's call stack has overflowed, and the JVM cannot allocate additional memory for method invocations.

Example:

In this example, when the main method is executed, it calls the recursiveMethod. The recursiveMethod is designed to call itself endlessly without any termination condition. As a result, the call stack keeps growing until it exceeds its maximum limit, throwing a StackOverflowError.

What is Heap Memory in Java?

Heap memory in Java is a memory region for dynamic memory allocation. It is the runtime data area where objects are assigned and released during program execution.

JVM manages it and serves as the primary repository for many application data, encompassing objects, arrays, and class instances. Heap memory assigns memory to objects upon creation and reclaims memory through garbage collection when objects become obsolete.

Example of Heap Memory in Java

public class HeapExample {
    public static void main(String[] args) {
        // Creating objects in the heap memory
        Person person1 = new Person("Manisha", 26);
        Person person2 = new Person("Aron", 26);

        // Modifying object properties
        person1.setAge(27);
        person2.setName("Aritra");

        // Printing object details
        System.out.println(person1.getName() + " is " + person1.getAge() + " years old.");
        System.out.println(person2.getName() + " is " + person2.getAge() + " years old.");
    }
}

class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

In this example, the heap memory is allocated for the objects created from the Person class. The objects persist in memory until they are no longer referenced, at which point the Java garbage collector deallocates the memory occupied by those objects. The heap memory allows for dynamic memory allocation and deallocation, providing flexibility for managing objects and data structures in Java programs.

When the main method is executed, two objects of the Person class, person1 and person2, are created using the new keyword. The memory for these objects is allocated on the heap. The constructor of the Person class is called for each object, initializing the name and age instance variables. The properties of the objects are modified using setter methods, changing the values stored in the heap memory. The getter (getAge and getName) methods retrieve the values from the objects in the heap memory. The object details, including the names and ages, are printed on the console.

Why OutOfMemoryError is thrown in Java?

An OutOfMemoryError occurs when the JVM cannot allocate any memory for a memory allocation request. This error occurs when the heap memory of the JVM is fully exhausted, resulting in the inability to allocate any additional memory for objects.

To address an OutOfMemoryError indicates that the application may necessitate optimizations or adjustments to the memory settings because it has reached its maximum memory capacity.

In this example, we have a List<Integer> called numbers. We attempt to continuously add elements with the value of 1 to the list inside an infinite loop. Eventually, the memory allocated for the heap will be exhausted, resulting in an OutOfMemoryError.

import java.util.ArrayList;
import java.util.List;

public class upGrad {
    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<>();

        try {
            while (true) {
                numbers.add(1);
            }
        } catch (OutOfMemoryError e) {
            System.out.println("Out of memory!");
        }
    }
}

Differences Between Stack and Heap Memory

Stack Memory

Heap Memory

Used for storing local variables and method calls

Used for dynamically allocating and deallocating objects

Operates in a last-in-first-out (LIFO) manner

Objects are allocated and deallocated dynamically

Limited in size

Provides a larger and more flexible storage space

Managed by the JVM automatically

Managed by the JVM through garbage collection

Faster access and deallocation

Slower access and deallocation

Stores primitive types and references

Stores objects, arrays, and class instances

Memory allocation and deallocation are automatic

Memory allocation and deallocation must be managed

Memory usage is determined at compile-time

Memory usage is determined at runtime

Memory space is smaller

Memory space is larger

Typically used for method invocations

Typically used for dynamically created objects

Advantages of using Stack Memory in Java

Some of the advantages of using Stack Memory in Java have been elucidated below:-

  • Efficiency: Stack memory offers faster allocation and deallocation.
  • Automatic Memory Management: The JVM automatically handles memory allocation and deallocation in stack memory.
  • Deterministic Lifetime: Objects stored in stack memory have a predictable lifespan and are automatically deallocated when they go out of scope.
  • Thread-Specific: Each thread in a Java application has its own stack memory, allowing for efficient thread-specific storage.

Disadvantages of using Stack Memory in Java

There are a few downsides to using Stack Memory in Java. They are:-

  • Limited Size: Stack memory has a restricted capacity, which may lead to stack overflow errors if memory usage exceeds the available space.
  • Fixed Memory Allocation: Stack memory size is determined at compile-time and cannot be dynamically adjusted during program execution.
  • No Dynamic Memory Allocation: Stack memory does not support dynamic memory allocation or deallocation of objects.

Advantages of using Heap Memory in Java

Some key benefits of using Heap Memory in Java are:-

  • Dynamic Memory Allocation: Heap memory enables dynamic allocation and deallocation of objects at runtime.
  • Flexibility: Heap memory provides a larger and more flexible storage space.
  • Object Persistence: Objects stored in heap memory have a longer lifespan and can be accessed from multiple methods or threads.
  • Dynamic Memory Management: Heap memory is managed by the JVM through garbage collection, automatically reclaiming memory from unused objects.

Disadvantages of using Heap Memory in Java

Here are some disadvantages of using Heap Memory in Java:-

  • Slower Access: Heap memory access can be slower than stack memory due to the dynamic allocation and deallocation process.
  • Fragmentation: Heap memory can experience fragmentation, leading to inefficient memory utilization.
  • Garbage Collection Overhead: Heap memory's automatic garbage collection process can impact application performance.
  • Memory Leaks: Improper heap memory management can result in memory leaks, causing unnecessary memory consumption.

Conclusion

Heap memory and stack memory are two essential components of memory management in Java. Heap memory provides flexible storage space for dynamically allocated objects, while stack memory efficiently handles method invocations and local variables. Understanding these memory concepts is crucial for developing efficient and optimized Java applications.

To learn more about these important Java concepts, consider taking up a course in upGrad to enhance your knowledge and skills in the fundamental and advanced concepts of Java programming.

FAQs

1. What is the key difference between heap memory and stack memory in Java?

The main difference between heap and stack memory in Java is that heap memory is used for dynamically allocating objects, while stack memory is used for storing local variables and method calls.

2, How is stack allocation different from heap allocation in Java?

Stack allocation refers to the automatic memory management of method invocations and local variables. On the other hand, heap allocation refers to the dynamic allocation and deallocation of objects.

3. Is heap memory allocated on the hard disk or in the RAM?

In Java, heap memory is allocated in the RAM (Random Access Memory) and not on the hard disk.

Take the Free Java Quiz & See Your Rank!

image
right-top-arrowright-top-arrow
Need More Help? Talk to an Expert
form image
+91
*
By clicking, I accept theT&Cand
Privacy Policy
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
right-top-arrowleft-top-arrow

upGrad Learner Support

Talk to our experts. We’re available 24/7.

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918045604032

Disclaimer

upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. .