memory management
Memory management is the practice of handing out regions of a computer’s memory to a running program and reclaiming them once the program no longer needs them. Every value a program creates occupies storage, and memory management decides where that storage comes from, how long it stays reserved, and when it returns to the pool of free space.
Most programs draw memory from two regions with different lifetimes. The call stack holds the local variables of active function calls and unwinds automatically as each call returns, so its bookkeeping costs almost nothing.
The heap holds values that must outlive the function that created them. Those lifetimes don’t follow any predictable order, so the heap needs an allocator that hands out blocks on request and tracks which ones are still in use.
Languages differ in who does the reclaiming:
- Manual: The programmer pairs every allocation with a matching release, as C does with
malloc()andfree(). This gives precise control over timing and memory footprint. - Automatic: A runtime reclaims memory on the program’s behalf, through garbage collection, reference counting, or a combination of the two, as in CPython.
- Ownership-based: The compiler infers each value’s lifetime from scope and ownership rules, then inserts the releases itself, as Rust does.
What separates the three is when the release lands. Step through the same small program below and watch the call stack push and pop frames on its own, while the heap block that outlives those frames waits for whichever release its language uses:
Errors in this area produce a well-known family of bugs. A memory leak keeps unreachable blocks reserved until the process exits, a dangling pointer reads a block that was already freed, and fragmentation leaves free space in pieces too small to satisfy a request.
Related Resources
Tutorial
Memory Management in Python
Get ready for a deep dive into the internals of Python to understand how it handles memory management. By the end of this article, you’ll know more about low-level computing, understand how Python abstracts lower-level operations, and find out about Python’s internal memory management algorithms.
For additional information on related topics, take a look at the following resources:
- How Python Manages Memory (Course)
- Pointers and Objects in Python (Course)
- Your Guide to the CPython Source Code (Tutorial)
- CPython Internals: Paperback Now Available! (Tutorial)
- Memory Management in Python (Quiz)
- Your Guide to the CPython Source Code (Quiz)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Martin Breuss • Updated Oct. 19, 2026