What memory allocation means
Memory allocation is the process of reserving part of a computer’s memory for a program to use, and releasing that part when the program no longer needs it. Every running program needs memory for its code, its variables and the data it creates while it runs. Allocation is how that memory is handed out and tracked.
Three kinds of allocation
Programmers often sort allocation into three groups, which follow where the memory lives.
- Static allocation reserves memory before the program starts running, usually for global or static variables. This memory typically lasts for the whole run of the program.
- Automatic allocation reserves memory when a function is called, for its local variables and parameters, and releases it when the function returns. In most languages this happens on the stack.
- Dynamic allocation reserves memory while the program runs, when the program asks for it, and keeps it until it is released. In most languages this happens on the heap.
The stack and heap are covered in more detail in the guide to the difference between stack and heap memory.
How a program gets memory
A program usually doesn’t work with the hardware directly. The operating system gives each running program its own address space, a range of memory addresses it can use. Inside that space, a runtime or a memory allocator decides which block belongs to which request. Many allocators ask the operating system for a larger chunk of memory once, then divide it into smaller blocks as the program asks for them.
Automatic allocation is simpler. The compiler generates the instructions that adjust the stack when a function is entered and when it exits, so the program doesn’t need a separate search for free space.
How memory gets freed
- Stack memory is freed automatically when a function returns.
- Manual freeing is used in languages such as C. The programmer calls a function such as
freeto return a block to the allocator. C++ can also release memory automatically when an object goes out of scope, through destructors. - Automatic freeing is done by a garbage collector in languages such as Java. It finds objects the program can no longer reach and reclaims them. Python uses reference counting along with a collector that handles reference cycles.
Dynamic allocation happens mostly on the heap, which raises a natural next question: why is heap memory used at all, when the stack is simpler?
Common allocation problems
- Memory leaks happen when memory is allocated and never released, so the program’s memory use keeps growing.
- Use after free happens when code keeps using a block after it has been released. This can crash the program or corrupt data.
- Double free happens when the same block is released twice.
- Fragmentation happens when free memory is split into many small gaps. A larger request can then fail even when the total free space looks large enough.
- Running out of memory happens when a program asks for more than is available.
Why allocation matters when you write code
Knowing how allocation works helps you reason about bugs, performance and limits. Local variables are cheap to create and go away on their own. Data created on demand needs an owner to release it, either you or the language runtime. That split is the reason the stack and heap behave so differently.
Key takeaways
- Memory allocation reserves memory for a program and releases it when it’s no longer needed.
- Allocation comes in three kinds: static, automatic and dynamic.
- Stack memory is freed for you on return. Heap memory is freed by hand in some languages and by a garbage collector in others.
- Most allocation bugs come from leaks, use after free, double free, fragmentation or running out of memory.