Skip to main content

Stack vs Heap

While a program runs, it stores data in two areas of memory: the stack and the heap. Knowing which data goes where explains how value types and reference types behave differently.

Diagram showing local variables in the stack and array data in the heap

The Stack​

Each time a method is called, a block of memory called a "frame" is pushed onto the stack. It holds that method's local variables. When the method returns, its frame is popped off and the memory is reused. Like a stack of plates, you can only add or remove from the top. This order is called LIFO (Last In, First Out).

stack

This is how memory is managed in methods because it's:

  • Fast 👉 allocation is just moving the "top" marker
  • Automatic 👉 memory is freed as soon as the method returns

The catch is that it's limited! The stack is small and fixed in size. Deep or infinite recursion can exhaust it, causing a "stack overflow" (hence the name of stackoverflow.com)

The Heap​

The heap is a larger, more flexible area for data whose size or lifetime isn't known in advance, such as arrays, lists and objects.

stack

This is how memory is managed for arrays and objects because it's:

  • Flexible 👉 space is allocated when the program runs, so the size doesn't have to be known ahead of time
  • Long lived 👉 heap data can outlive the method that created it. It is freed later by the "garbage collector" once nothing references it

The variable on the stack stores only a reference, which is the address of that heap data.

Example: Variable Storage​

Given the code below, each type of variable lives in a different part of memory:

int numberA = 5; // value stored directly on the stack
int[] myArray = { 44, 3 }; // reference on the stack, array data on the heap

Diagram: count and scores on the stack, with scores pointing to an array on the heap

Analogy​

Creating an Array​

The illustration below, by teacher Sandy Bultena, shows what is actually stored in an array variable.

Illustrated analogy: a shopper's cart holds a ticket pointing to soil stored in a warehouse

A shopper (the program) wants to keep a large amount of soil (the array data), but it won't fit in their shopping cart (the stack). Instead, the soil is stored in a warehouse (the heap), and the shopper keeps a ticket with its location (the reference).

Passing an Array​

The same analogy carries over to passing an array to a method!

Illustrated analogy: the shopper hands a copy of their warehouse ticket to a landscape contractor, and both tickets point to the same soil

The shopper (the program) hires a landscape contractor (the method) to do a job, and hands over the supplies it needs (the arguments). The soil itself stays in the warehouse. The contractor just gets a copy of the ticket (a copy of the reference).

Since both tickets point to the same soil, any changes the contractor makes to it are still there when the shopper goes back to the warehouse:

static void ContractorUseSoil(int[] soil) {
soil[0] = 99; // changes the array on the heap
}

int[] soil = new int[1000]; // so much dirt
ContractorUseSoil(soil); // soil[0] is now 99

References​

https://blog.stackademic.com/how-java-memory-works-c751460e3cbd