Heap & Stack in Java

Absolute programmer
4 min readDec 18, 2021

--

Introduction

When a java program start running, it will be allocated a big chunk of memory. There are mainly two type of memory will be used in Java which is Heap and Stack. They will be used for storing different data.

Overview

The stack is mainly for storing variables in local scope and method calls while heap is for storing objects. In this article, the concept behind stack and heap will be explained in the following ways:

  1. Explain the concept and operation behind stack and heap

2. Provide a code snippet for example

3. Illustrate the result of the code snippet through a couple of diagrams

Stack

The stack will be used for storing methods calls and local variables including parameters passing into methods. A stack frame will be allocated a portion of the stack memory when a method is called. Then, the stack frame will be pushed into the stack.

The stack frame will keep tracking the line of code executing and storing local variables when the line of code creates one. The top of the stack is always the stack frame in which have the method running. When the method calls another method, a new stack frame for that new method call will be created and pushed to the top of the stack. The new stack frame will then be executing switching from the second stack frame so the top of the stack is always the stack frame executing by the JVM.

Once the method is finished running which means all the code inside the curly brace have been executed. The stack will pop that stack frame and start executing the next stack frame placed on the top.

Stack illustration diagram

Situation

Here is the code snippet on the stack explanation.

public void one() {    
int y = 0;
two(1);
}
public void two(int a) {
int x = x + a;
three();
four(); }
public void three() {
//more code here
}
public void four(){
//more code here
}
Procedure diagram on the stack code snippet above

Heap

The heap stores all the objects. When an object is created by new operator, an object will first be allocated memory in the heap. Then, a unique memory address will be returned to access the method and its instance variable and the address will be assigned to the reference variable. The reference variable is a local variable which lives in the stack but it can access the object in the heap. Therefore, an object can be shared by different stack frames through passing the reference variable as parameters or share through assigning the address to a new reference variable.

If two reference variables point to the same object, any change on one object’s instance variable will be reflected on all the reference variables pointing to that same object.

Heap illustration diagram

Situation

Here is the code snippet on the heap explanation.

public void one() {  JavaObject javaObject = new JavaObject();  javaObject.setValue(1);  System.out.println(“javaObject value Before: “ + javaObject.getValue() );  two(javaObject);  System.out.println(“javaObject value After: “ + javaObject.getValue() ); } public void two(JavaObject j2) {  j2.setValue(2); } class JavaObject {  int value = 0; public void setValue(int value){  this.value = value; } public int getValue(){  return this.value;  } }
Procedure diagram on the heap code snippet above(1)
Procedure diagram on the heap code snippet above(2)

Conclusion

It is important to know that how memory is used in Java as it is a building block on other programming topics, such as multi-threading, variable scope and memory management. All in all, the stack saves the method call and local variables and the heap saves the object. Moreover, the access to an object can be shared by passing the reference variable around.

--

--