Open In App

Java Memory Management

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

Java memory management is a fundamental concept that involves the automatic allocation and deallocation of objects, managed by the Java Virtual Machine (JVM). The JVM uses a garbage collector to automatically remove unused objects, freeing up memory in the background. This eliminates the need for developers to manually handle memory management. This article will focus on Memory management in Java, how the heap works, reference types, garbage collection, and related concepts.

Why Learn Java Memory Management?

Java automatically manages memory with the help of the garbage collector, so programmers do not need to manually destroy objects. But, not all memory are managed equally by the garbage collector, and understanding memory management helps create efficient programs and debug crashes effectively.

JVM Memory Structure

JVM defines various runtime data areas used during the execution of a program. Some of the areas are created by the JVM, whereas some are created by the threads that are used in a program. However, the memory area created by the JVM is destroyed only when the JVM exits. The data areas of a thread are created during instantiation and destroyed when the thread exits. These areas include:

  • Heap Area
  • Method Area
  • JVM Stacks
  • Native Method Stacks
  • Program Counter (PC) Registers

Java Virtual Machine (JVM) Memory Areas

The image below demonstrates the Java Memory Area parts:

Java-Memory-Area-Parts


1. Heap Area

  • Heap is a shared runtime data area where objects and arrays are stored. It is created when the JVM starts.
  • The memory in the heap is allocated for all the class instances and arrays.
  • Heap can be of fixed or dynamic size depending upon the system’s configuration.
  • JVM allows user to adjust the heap size. When the new keyword is used the object is allocated in the heap and its reference is stored in the stack.
  • There exists one and only one heap for a running JVM process.

Scanner sc = new Scanner(System.in)

Here, the Scanner object is stored in the heap and the reference sc is stored in the stack

Note: Garbage collection in heap area is mandatory.

2. Method Area

  • Method area is a logical part of the heap and it is created when the JVM starts.
  • Method area is used to store class-level information such as class structures, Method bytecode, Static variables, Constant pool, Interfaces.
  • Method area can be of fixed or dynamic size depending on the system's configuration.
  • Static variables are stored in the Stack.
  • Garbage collection of the method area is not guaranteed and depends on JVM implementation.

Note: Method area is logically a part of heap, many JVM like HotSpot uses a separate space known as Metaspace outside the heap to store it.

Example:

Java
// Java Program to demonstrate how java variables
// are stored in the different memory areas
import java.io.*;

class Geeks {
  
    // static variables are stored in the Method Area
    static int v = 100;

    // instance variables are stored in the Heap
    int i = 10;

    public void Display()
    {
        // local variables are stored in the Stack
        int s = 20;

        System.out.println(v);
        System.out.println(s);
    }
}

public class Main {
    public static void main(String[] args) {
        Geeks g = new Geeks();
      
        // Calling the Display method
        g.Display();
    }
}

Output
100
20

Explanation: In the above code, we define a class Geeks with static, instance, and local variables to demonstrate how they are stored in different memory areas (Method Area, Heap, and Stack) and print their values through the Display method.

Note:

  • Static variables are stored in the Method Area.
  • Instance variables are stored in the Heap.
  • Local Variables are stored Stack.

3. JVM Stacks

  • A stack is created when a thread is created, and the JVM stack is used to store method execution data, including local variables, method arguments, and return addresses
  • Each Thread has its own stack, ensuring thread safety.
  • Stacks size can be either fixed or dynamic, and it can be set when the stack is created.
  • The memory for stack needs not to be contiguous.
  • Once a method completes execution, its associated stack frame is removed automatically.

4. Native Method Stacks

  • Native method stack is also known as C stacks.
  • Native method stacks are not written in Java language
  • This memory is allocated for each thread when it is created and can have either a fixed or dynamic size.
  • Native method stacks handle the execution of native methods that interact with the Java code.

5. Program Counter (PC) Registers

Each JVM thread which carries out the task of a specific method has a program counter register associated with it. The non native method has a PC which stores the address of the available JVM instruction whereas in a native method, the value of program counter is undefined. PC register is capable of storing the return address or a native pointer on some specific platform.

Working of a Garbage Collector

The Garbage collector in Java automatically removes the unused objects that are no longer needed. It runs in the background to free up memory.

  • Garbage collector finds objects that are no longer needed by the program.
  • It removes those unused objects to free up the memory and making space for new objects.
  • Java uses generational garbage collection so that new objects are collected more frequently in the young generation than older objects which survive longer in the old generation, this helps improve efficiency.
  • You can request garbage collection using System.gc(), but the JVM ultimately decides when it should run.

Note: System.gc() and Runtime.gc() are the methods which requests for Garbage collection to JVM explicitly but it doesn’t ensures garbage collection as the final decision of garbage collection is of JVM only.


Next Article
Article Tags :
Practice Tags :

Similar Reads