# Stack Memory

> A small, fast part of memory. It holds the values of running functions.

**Field:** Software Development  
**Category:** Compilers & Runtimes  
**Also known as:** Call Stack, The Stack, Stack Overflow

## Think of it like

Think of it like a pile of plates. You add to the top and take from the top.

## Definition

Stack memory is a small, fast area that holds the values of the functions running now. Each call to a function puts a new block on top of the stack. The block holds that function's local variables. When the function returns, its block comes off the top at once. On Linux the stack for a program's main thread is usually 8 MB. So a function that calls too deep causes a stack overflow.

## Example

Lucy's function calls itself with no stopping point. After many thousands of calls the stack is full. The program crashes with a stack overflow.

## Example code

```console
$ ulimit -s
8192
```

On this Linux machine, each program's main stack can grow to 8,192 KB, or 8 MB.

## Why it matters

It is why local values are cheap and vanish after a function ends. It is also why deep recursion can crash.

## Common mistake

Putting a huge array in a local variable in C. It may not fit on the stack, so it belongs on the heap.

## Three principles

- **Last in, first out.** Like a pile of plates.
- **Freed on return.** Nothing to clean up.
- **Small and fast.** Often 8 MB on Linux.

## Related terms

- [Heap Memory](https://learn.benibix.com/term/development/heap-memory)
- [Memory Management](https://learn.benibix.com/term/development/memory-management)
- [Recursion](https://learn.benibix.com/term/development/recursion)
- [Stack Trace](https://learn.benibix.com/term/development/stack-trace)

## Told apart from

- [Stack vs Heap Memory](https://learn.benibix.com/compare/stack-vs-heap-memory): Stack memory and heap memory are both parts of a running program's memory, but they hold different data. The stack is small and fast. It holds the local values of the functions running now, and they vanish when the function returns. The heap is large and slower. It holds data that must live longer or grow, until it is freed.

---

Source: https://learn.benibix.com/term/development/stack-memory
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
