Stack and Heap Memory

Difference Between Stack and Heap Memory

Learn the difference between stack and heap memory: what each stores, how it's allocated and freed, how fast each is, and the errors each can cause.

On this page
  1. The short answer
  2. What the stack holds
  3. What the heap holds
  4. Stack vs heap at a glance
  5. How the stack and heap work together
  6. Common errors from each
  7. Does your language decide for you?
  8. Key takeaways

The short answer

The stack holds the data for the function calls a program is running right now, such as local variables and the information needed to return from each call. It works in last-in, first-out order, and its memory is reclaimed automatically when a function returns. The heap is a larger pool for data a program requests while it runs. That data stays in use until it is freed, either by the program or by a garbage collector, so its lifetime doesn’t have to match a single function call.

What the stack holds

Each function call gets a block of memory on the stack, usually called a stack frame. A frame typically holds:

  • the function’s parameters and local variables
  • the return address, so the program knows where to continue when the call ends
  • temporary values the function needs while it runs

When the function returns, its frame is removed and the space is reused by the next call. Nothing has to be freed one item at a time, which keeps stack allocation simple and usually quick. Each thread in a program normally gets its own stack, and the stack’s size is limited. Very deep recursion can use up that space. If recursion is new to you, see what is a recursive function first.

The stack used for function calls is a memory region. It is not the same thing as the stack data structure covered in stack operations in data structure, although the two share a name and a last-in, first-out idea.

What the heap holds

The heap holds data whose size or lifetime isn’t known when the code is written, or that has to live longer than the function that created it. Examples include objects in Java or Python, a list that grows as items are added, and the nodes of a linked list.

Allocating on the heap usually means asking a memory allocator for a block of a given size. The block stays in use until someone frees it. If the idea of a program requesting memory is new, read about what is memory allocation before going further. In C and C++, the programmer calls a function such as free or delete. In languages such as Java and Python, a garbage collector finds data that nothing refers to anymore and reclaims it. The heap is shared across the whole program, including across threads.

Stack vs heap at a glance

Stack Heap
Managed by Automatically, as functions are called and return The programmer or a garbage collector
Stores Local variables, parameters and return addresses Dynamically allocated objects and data
Lifetime Until the function returns Until the data is freed or no longer referenced
Order Last in, first out No fixed order
Size Limited, set per thread Generally larger, limited by available memory
Speed Usually quicker to allocate and free Usually slower, since the allocator has to find space
Shared between threads? No, each thread has its own stack Yes
Common errors Stack overflow Memory leaks, dangling pointers, running out of memory

How the stack and heap work together

The two regions often cooperate in the same program. Take this Java line:

Person p = new Person("Ada");

The variable p is a local variable, so the reference it holds sits in the method’s stack frame. The Person object is created with new, so it lives on the heap. The stack holds only the address that points to the object. The JVM may store things differently in practice, so treat this as a common way to picture the layout rather than a guarantee. This ties into primitive vs non primitive data types, since primitives and objects are handled differently.

Common errors from each

  • Stack overflow happens when calls nest too deeply, for example through recursion with no proper stopping point, and the stack runs out of room.
  • Memory leaks happen when heap memory is no longer needed but is never freed. In languages where the programmer frees memory by hand, this is a common bug. Even with a garbage collector, objects can stay alive because the program still holds references to them.
  • Dangling pointers happen when code uses memory after it has been freed. In C, returning a pointer to a local variable is a bug, because that variable’s stack frame is gone once the function returns.
  • Running out of heap memory happens when a program keeps allocating more than the available space.

Does your language decide for you?

Languages differ in how much control they give you.

  • C and C++ let you choose directly. Local variables usually go on the stack, and memory from malloc or new goes on the heap.
  • Java keeps local primitive values and references in the stack frames of method calls, creates objects on the heap, and uses a garbage collector to reclaim them.
  • Python stores objects on the heap and manages them with reference counting along with a garbage collector that handles cycles.

Key takeaways

  • The stack serves the current chain of function calls, and its memory is reclaimed automatically when each call returns.
  • The heap serves data with a flexible size or lifetime. Something has to free it, either the programmer or a garbage collector.
  • The stack is usually faster and smaller. The heap is larger and more flexible, but it takes more work to manage.

Keep exploring

Guides from other topics

All topics →