Last Updated : 23 Jul, 2025
In Java, memory management is handled by the Java Virtual Machine (JVM). The breakdown of how objects are stored in memory:
The Java Virtual Machine is responsible for memory management in Java and divides memory into several areas:
Now, let us go through each of the memory areas in detail.
1. Heap MemoryHeap memory is a part of Java memory management. When an object is created using the new keyword, Java allocates space for that object in the heap. The amount of memory allocated depends on the fields defined in the object's class and their respective data types.
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 the heap area is mandatory for automatic memory management.
Heap Memory is divided into several regions:
metadata
Objects are stored after surviving multiple garbage collection cycles.Starting with Java 8, the old permanent generation space was removed and replaced by metaspace. Metaspace uses native memory outside the main heap. This change helps Java to handle class metadata better and also reduces the chances of errors caused by permanent generation.
When the new keyword is called, the JVM allocates memory for that object in the heap. The garbage collector automatically deletes objects that are no longer needed.
Example:
Java
// Java program to demonstrate where the
// objects are stored in the memory
import java.io.*;
class Student {
String name;
int age;
Student(String name, int age)
{
this.name = name;
this.age = age;
}
}
public class Geeks {
public static void main(String[] args)
{
// Here the student object will be stored in the
// heap memory and the references will be stored in
// the stack memory
Student s = new Student("bob", 20);
}
}
Explanation: Here, the Student object is allocated in the heap memory. The reference s which points to the object is stored in the stack memory.
2. Stack MemoryIn Java, Stack memory is used to store local variables, method calls and references to an object. Each time a method is called, a new stack frame is created to hold local variables and object references. The stack memory is managed automatically, when a method finishes the stack frame is removed and the space used by its local variable is also released.
Note:
// Java program to demonstrate where
// the objects are stored in the memory
import java.io.*;
public class Geeks {
// method parameter 'n' is stored in the stack
public static int calculate(int n)
{
// local varaible 'ans' is stored in the stack
int ans = n * 10;
return ans;
}
public static void main(String[] args)
{
// local variable 'n' is stored in the stack
int n = 10;
// n is passed as an argument so its reference is
// stored in the stack
calculate(n);
}
}
Note: Local variables, primitive data types like int, double, etc. are stored in the stack when declared as local variables. However, the objects are stored in the heap and their references are stored in the stack.
In Java, The garbage collector is responsible for reclaiming memory by destroying objects that are no longer in use. When an object becomes unreachable that means no active reference points to it, it then becomes eligible for garbage collector. The garbage collector removes those objects freeing up the memory in the heap.
Types of Garbage CollectorsIn Java, an object consist of three main parts:
The object header includes:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4