Java Stack Memory and Heap Memory
3 min readJul 22, 2023
Java Stack Memory:
- Stack memory is used for storing local variables and function call information.
- It holds the values of primitive type variables, as you mentioned, but it also stores references to objects.
- Each thread in a Java application has its own stack, which is used for method invocations and managing local variables.
- Stack memory is a limited and generally smaller memory region compared to the heap.
- It operates in a Last-In-First-Out (LIFO) manner, meaning the most recently called method is the first one to be removed.
- When a method is called, a new frame is pushed onto the stack to hold the method’s local variables and data.
- When the method completes execution, its frame is popped off the stack, and the memory is freed.
Java Heap Memory:
- Heap memory is used for dynamic memory allocation to objects.
- It stores non-primitive objects, which are instances of classes and arrays.
- All objects are created in the heap and persist there until they are no longer referenced.
- Unlike stack memory, the heap memory is shared among all threads in a Java application.
- Memory allocation and deallocation on the heap are managed by the garbage collector, which automatically frees up memory occupied by objects that are no longer in use.
- The Java Virtual Machine (JVM) divides the heap into several regions, such as the Young Generation and Old Generation, to optimize memory management.
Reference Variables:
- Reference variables to objects are indeed stored on the stack, but the objects themselves (the data they point to) are stored in the heap.
- When you declare a reference variable in Java, memory is allocated for that variable on the stack, and it holds the memory address of the corresponding object in the heap.
String Constant Pool:
- In earlier versions of Java, the String constant pool used to reside in the PermGen (permanent generation) space of the heap. However, starting from Java 8, the PermGen space was removed, and the String constant pool was moved to the heap itself.
- The String constant pool is a special area in the heap where String literals (e.g., “hello”, “world”) and interned Strings are stored. Interned Strings are those created using the String.intern() method.
- The String constant pool allows Java to reuse the same instances of String literals, which helps in memory optimization.
Coding Example
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void displayInfo() {
System.out.println("Name: " + name + ", Age: " + age);
}
public static void main(String[] args) {
// Example 1: Creating a person object on the heap
Person person1 = new Person("Mohosin", 25);
person1.displayInfo(); // Output: Name: Mohosin, Age: 25
// Example 2: Creating a person object on the heap
Person person2 = new Person("Kalam", 25);
person2.displayInfo(); // Output: Name: Kalam, Age: 30
// Example 3: Creating a person object on the heap and storing its reference in a local variable
Person person3; // This reference is stored in stack memory
person3 = new Person("Jahangir", 35);
person3.displayInfo(); // Output: Name: Jahangir, Age: 35
// Example 4: Storing a reference to a person object in a local variable (stack memory)
Person person4 = person1;
person4.displayInfo(); // Output: Name: Mohosin, Age: 25 (same object as person1)
// Example 5: Storing a reference to a person object in a local variable (stack memory)
Person person5 = person2;
person5.displayInfo(); // Output: Name: Kalam, Age: 30 (same object as person2)
}
}
Happy Coding 🙂