Heap Memory
- In Turkish
- Heap Belleği
- Pronunciation
- HEEP MEM-uh-ree
In short
Heap memory is the region for data a program allocates at runtime, whose size or lifetime isn't known in advance and can outlive the function that made it.
What is heap memory?
Unlike the stack, the heap has no fixed order. A program asks for a block of memory when it needs one, with malloc in C or new in C++ and Java, or implicitly whenever it creates an object, list or string in languages such as Python and JavaScript, and the block stays valid until it is released. That makes the heap the place for large data, data whose size changes, and data shared between functions.
Someone has to free that memory. In C and C++ the programmer does, with free or delete, and forgetting causes memory leaks, while freeing too early causes use-after-free bugs, a major source of security holes. Rust tracks ownership at compile time to free memory safely, and languages such as Java, C#, Python, Go and JavaScript use a garbage collector that finds and frees objects no longer reachable.
Heap allocation is slower than stack allocation: the allocator has to find a free block of the right size, keep track of what is in use, and deal with fragmentation, where free memory is split into gaps too small to use. Performance-sensitive code therefore reuses objects, allocates in batches or keeps small, short-lived values on the stack.
A common misconception is that the heap memory region has something to do with the heap data structure. They only share a name: the memory heap is a pool managed by an allocator, while the heap data structure is a tree used for priority queues.
Key takeaways
- The heap holds data allocated at runtime with flexible size and lifetime.
- Objects that outlive a function call live on the heap.
- C and C++ free it manually; Rust uses ownership; others use garbage collection.
- Heap allocation is slower than the stack and can fragment.
- The memory heap is unrelated to the heap data structure.
Example
#include <stdlib.h>
#include <string.h>
char *make_greeting(const char *name) {
char local[16] = "Hello, "; // stack: gone when the function returns
size_t len = strlen(local) + strlen(name) + 1;
char *greeting = malloc(len); // heap: survives the return
if (!greeting) return NULL;
strcpy(greeting, local);
strcat(greeting, name);
return greeting; // the caller now owns this memory
}
int main(void) {
char *g = make_greeting("Ada");
/* ... use g ... */
free(g); // forget this and you have a memory leak
return 0;
}Readers ask
When is memory allocated on the heap?
When its size isn't known at compile time, when it is large, or when it must outlive the function that created it. In managed languages such as Java, Python and JavaScript, most objects are heap-allocated automatically.
What is a memory leak on the heap?
Heap memory that is no longer needed but never freed, because the program forgot to release it or still keeps a reference to it. The program's memory use then grows over time.
Is the heap the same as the heap data structure?
No. The memory heap is a region of memory managed by an allocator. The heap data structure is a kind of tree that keeps the smallest or largest element at the top. They simply share a name.
Often compared
See also
- Stack MemoryOperating Systems, p. 28Stack memory is where a thread keeps its functions' local variables and return addresses, growing with each call and shrinking automatically on return.
- Garbage CollectionProgramming Fundamentals, p. 23Garbage collection is automatic memory management in which the language runtime finds data a program can no longer use and frees that memory for reuse.
- Memory LeakOperating Systems, p. 19A memory leak is a bug where a program keeps holding memory it no longer needs, so its memory usage grows over time and can slow down or crash the system.
- PointerProgramming Fundamentals, p. 44A pointer is a variable that stores the memory address of another value instead of the value itself, letting code read or change that data indirectly.
- Virtual MemoryOperating Systems, p. 37Virtual memory is an operating system technique that gives each process its own private address space and maps it to physical RAM or disk behind the scenes.
- HeapData Structures, p. 19A heap is a tree-based data structure that keeps the smallest or largest item at its root, so you can read it in O(1) and remove it in O(log n) time.
Spotted a mistake or something missing on this page?Suggest an edit