Stack vs Heap Memory Allocation: Speed Comparison and Implementation Details
This article explains how memory allocation on the stack is faster than on the heap, demonstrates the process with assembly instructions, provides benchmark code comparing allocation speeds, discusses compiler optimizations, and outlines the differences, advantages, and pitfalls of stack and heap memory usage.
Hello everyone, I'm Xiao Feng.
Stack Memory Allocation and Deallocation
Undoubtedly, allocating memory on the stack is faster because it only moves the stack pointer. On x86, a single instruction sub $0x40,%rsp moves the stack pointer down by 64 bytes, effectively allocating 64 bytes.
The deallocation is equally simple: the leave instruction restores the previous frame pointer and stack pointer, releasing the stack frame.
Heap Memory Allocation and Deallocation
Heap allocation is more complex; it requires a memory allocator that tracks free and used blocks, expands the heap when needed, and avoids fragmentation. The article references two detailed posts on implementing malloc and the underlying mechanisms.
A benchmark program compares stack vs heap allocation by calling two functions 100 million times each, measuring execution time.
void test_on_stack(){
int a = 10;
}
void test_on_heap(){
int* a = (int*)malloc(sizeof(int));
*a = 10;
free(a);
}
void test(){
auto begin = GetTimeStampInUs();
for(int i=0;i<100000000;++i){
test_on_stack();
}
cout<<"test on stack "<<((GetTimeStampInUs() - begin)/1000000.0)<Without compiler optimizations the stack version takes about 0.2 s while the heap version takes about 20 s, a hundred‑fold difference. With optimizations both compile to empty functions, reducing the difference to a few milliseconds.
test_on_stack:
400f85: 55 push %rbp
400f86: 48 89 e5 mov %rsp,%rbp
400f89: 5d pop %rbp
400f8a: c3 retq
test_on_heap:
400f8b: 55 push %rbp
400f8c: 48 89 e5 mov %rsp,%rbp
400f8f: 5d pop %rbp
400f90: c3 retqThe compiler removes the unused variable assignments, turning both functions into no‑ops, which explains the near‑identical timings after optimization.
Differences Between Stack and Heap Memory
The stack is a LIFO structure automatically managed by the compiler; its lifetime is bound to function calls, and its size is limited, leading to possible stack overflow for large allocations.
The heap is manually managed by the programmer; it offers flexible lifetimes but requires explicit allocation and deallocation, and can suffer from leaks and fragmentation.
Conclusion
Stack allocation is automatically managed and generally faster, while heap allocation provides flexibility at the cost of manual management. Use stack memory when possible, and resort to heap memory for larger or dynamic lifetimes.
Refining Core Development Skills
Fei has over 10 years of development experience at Tencent and Sogou. Through this account, he shares his deep insights on performance.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.