Comparisons and explanations · Introductory

Compare stacks and heaps using the JVM execution model

Distinguish invocation frames from shared objects, then separate references, lifetime, and logical runtime areas from physical optimization.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Specify which meanings are being compared
  2. A JVM stack holds invocation frames
  3. Separate references from the objects they identify
  4. Use errors and measurements with the model
  5. Ask Logeia about this articleCheck your understanding

Specify which meanings are being compared

Stack can name a last-in-first-out data structure or an area associated with calls. Heap can name a priority-queue structure or a runtime memory area. This lesson compares method execution and object storage using the Java Virtual Machine specification, rather than mixing these unrelated meanings.

Different languages and runtimes manage memory differently. Avoid claiming that every local variable in every optimized program must occupy a physical machine stack. Start with the logical model specified by the runtime, then investigate physical placement and optimization as separate implementation questions.

A JVM stack holds invocation frames

Each JVM thread has its own Java Virtual Machine stack. Method invocations create frames containing information needed for execution, including local variables and an operand stack. When an invocation completes, its frame has completed its role. This model helps explain nested calls and recursion.

A specification frame does not map one-to-one to a fixed instruction sequence or mandatory physical layout. JIT compilation can inline calls or retain values differently while preserving program behavior. The JVM specification deliberately describes an abstract machine rather than one required implementation strategy.

Separate references from the objects they identify

The JVM heap is the logical area for class instances and arrays and is shared among threads. If local variables a and b reference the same array, modifying an element through b can be observed through a. Two variables do not automatically mean that two independent copies of the array exist.

Completing a method also does not imply immediately destroying every object created inside it. An object can remain usable through a reference retained by its caller or another reachable object. Conversely, becoming unreachable does not promise collection at a particular observable instant. Reachability, invocation lifetime, and collection timing are distinct concepts.

Use errors and measurements with the model

Deep recursion that creates invocation state and a large retained object graph suggest different investigations. Distinguish StackOverflowError from OutOfMemoryError and inspect the relevant runtime condition rather than summarizing every failure as the same kind of memory shortage.

Implementations may optimize allocation while preserving externally visible behavior. An object-creation expression alone cannot prove a particular physical placement in an optimized program. “Stack is always fast and heap is always slow” is therefore an inadequate explanation. Use lifetime, sharing, allocation, and reachability to ask precise questions about the runtime and the workload.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Build your CS foundations · STEP 4

Understand the systems behind the web · STEP 2

Prepare for AI systems · STEP 3

Choose a learning path