
Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:
Once the early-adopter seats are all used, the price will go up and stay at $33/year.
Last updated: July 1, 2024
To run an application in an optimal way, JVM divides memory into stack and heap memory. Whenever we declare new variables and objects, call a new method, declare a String, or perform similar operations, JVM designates memory to these operations from either Stack Memory or Heap Space.
In this tutorial, we’ll examine these memory models. First, we’ll explore their key features. Then we’ll learn how they are stored in RAM, and where to use them. Finally, we’ll discuss the key differences between them.
Stack Memory in Java is used for static memory allocation and the execution of a thread. It contains primitive values that are specific to a method and references to objects referred from the method that are in a heap.
Access to this memory is in Last-In-First-Out (LIFO) order. Whenever we call a new method, a new block is created on top of the stack which contains values specific to that method, like primitive variables and references to objects.
When the method finishes execution, its corresponding stack frame is flushed, the flow goes back to the calling method, and space becomes available for the next method.
Some other features of stack memory include:
Heap space is used for the dynamic memory allocation of Java objects and JRE classes at runtime. New objects are always created in heap space, and the references to these objects are stored in stack memory.
These objects have global access and we can access them from anywhere in the application.
We can break this memory model down into smaller parts, called generations, which are:
These different portions are also discussed in the article Difference Between JVM, JRE, and JDK.
We can always manipulate the size of heap memory as per our requirement. For more information, visit this linked Baeldung article.
Some other features of heap space include:
Based on what we’ve learned so far, let’s analyze a simple Java code to assess how memory is managed:
class Person {
int id;
String name;
public Person(int id, String name) {
this.id = id;
this.name = name;
}
}
public class PersonBuilder {
private static Person buildPerson(int id, String name) {
return new Person(id, name);
}
public static void main(String[] args) {
int id = 23;
String name = "John";
Person person = null;
person = buildPerson(id, name);
}
}
Let’s analyze this step-by-step:
Let’s look at this allocation in the diagram below:
Before we conclude this article, let’s quickly summarize the differences between the Stack Memory and the Heap Space:
Parameter | Stack Memory | Heap Space |
---|---|---|
Application | Stack is used in parts, one at a time during execution of a thread | The entire application uses Heap space during runtime |
Size | Stack has size limits depending upon OS, and is usually smaller than Heap | There is no size limit on Heap |
Storage | Stores only primitive variables and references to objects that are created in Heap Space | All the newly created objects are stored here |
Order | It’s accessed using Last-in First-out (LIFO) memory allocation system | This memory is accessed via complex memory management techniques that include Young Generation, Old or Tenured Generation, and Permanent Generation. |
Life | Stack memory only exists as long as the current method is running | Heap space exists as long as the application runs |
Efficiency | Much faster to allocate when compared to heap | Slower to allocate when compared to stack |
Allocation/Deallocation | This Memory is automatically allocated and deallocated when a method is called and returned, respectively | Heap space is allocated when new objects are created and deallocated by Gargabe Collector when they’re no longer referenced |
Stack and heap are two ways in which Java allocates memory. In this article, we learned how they work, and when to use them for developing better Java programs.
To learn more about Memory Management in Java, have a look at this article here. We also touched on the JVM Garbage Collector, which is discussed briefly over in this article.