Stack vs Heap

Analogy: the stack is like a stack of books.

Stack

  • Simpler and faster, but more limited.
  • Holds method frames, local variables, value types.
  • Memory is reclaimed automatically when a frame exits.

Heap

  • Slower and a bit more complex, but allows more sophisticated structures.
  • Objects can outlive the call stack (reference types).
  • Static variables also live here.

Notes

  • Async methods may run on different threads — relevant to heap-allocated state lifetimes.
  • This is language/runtime-dependent (the note is framed around .NET/JS-style value vs reference).

Open questions

  • Garbage collection & stack vs heap in our runtime (Node/V8).
  • Stack overflow causes; object lifetime & closures.