Sub topic 2
Stack Frames and the Heap
This chapter assumes you know basic Java syntax, object-oriented programming, references, and how to run Java programs from the command line. It focuses on the working difference between a thread's stack frames and the shared heap.
Two places with different jobs
When a Java program runs, the Java Virtual Machine (JVM) uses several runtime memory areas. For everyday object allocation, the important distinction is:
- A stack frame stores the state of one method call.
- The heap stores objects and arrays.
- A reference is a value that identifies an object. A reference can be stored in a stack frame, while the object it identifies is stored in the heap.
Each thread has its own Java stack. Threads cannot directly use another thread's stack frames. The heap is shared by all threads in the JVM.
| Memory area | Typical contents | Lifetime |
|---|---|---|
| Stack frame | Parameters, local variables, operand data, return information | One method call |
| Heap | Objects and arrays | Until no GC root can reach them |
| Metaspace | Class metadata | Until the class is unloaded |
The stack is organized as last in, first out. Calling a method creates a frame. Returning from the method removes that frame.
How a method call works
Consider this program:
public class StackHeapDemo {
public static void main(String[] args) {
Person person = createPerson("Mina");
System.out.println(person.name);
}
static Person createPerson(String name) {
Person result = new Person(name);
return result;
}
static class Person {
String name;
Person(String name) {
this.name = name;
}
}
}When main starts, the JVM creates a frame for main. That frame contains the args parameter and later the local reference named person.
Calling createPerson creates another frame. Its parameter name refers to the string "Mina". The expression new Person(name) creates a Person object on the heap. The local variable result contains a reference to that object.
When createPerson returns, its frame disappears. The Person object remains on the heap because person in the main frame now refers to it. Returning a reference does not move the object from the heap to the stack.
The name field in the Person object is also a reference. It refers to a String object. The field is stored as part of the heap object, even though its value identifies another object.
References do not copy objects
Assignment copies a reference value, not the object:
Person first = new Person("Mina");
Person second = first;
second.name = "Noor";
System.out.println(first.name); // NoorBoth local variables refer to the same heap object. Changing the object through one reference is visible through the other.
A method parameter behaves similarly. Java passes the reference value by value:
static void rename(Person person) {
person.name = "Kai"; // changes the shared object
person = new Person("Lee"); // changes only this local parameter
}The assignment to person.name affects the caller's object. Reassigning the parameter does not change the caller's variable.
Recursion and stack exhaustion
Every active recursive call needs its own frame:
public class RecursionDemo {
static void countDown(int n) {
if (n == 0) {
return;
}
countDown(n - 1);
}
public static void main(String[] args) {
countDown(3);
}
}At the deepest point, frames for main and four calls to countDown are active. If recursion continues too deeply, the stack cannot create another frame and the JVM throws StackOverflowError.
A large number of heap objects usually causes garbage-collection activity or OutOfMemoryError: Java heap space. These are different failures from stack exhaustion.
Exercise
After Person second = first, what happens when second.name = "Noor" is executed, and why?
Show answer
Both variables refer to the same heap object, so changing its field through second also changes what first.name reads.