by adrian_b · 1w ago · 0 comments
Nonetheless, the title of the paper by Andrew Appel (Garbage Collection Can Be Faster Than Stack Allocation) was just a false statement, which today would be called a clickbait title.
No method of dynamic memory allocation can be faster than stack allocation, if the latter is done correctly.
In Appel's paper, he claimed that if you have tons of memory, than the overhead of freeing memory by the garbage collector can become low, because of its laziness, i.e. the garbage collector will be invoked only when there is a huge amount of memory that must be freed, so the overhead per freed byte will be low, even lower than when stack-allocated memory is freed word-by-word.
However, this comparison is a classic example of a straw man. When memory is abundant, many compilers implement an optimization where stack deallocation is delayed as much as possible, in order to free with a single instruction large amounts of memory. So the same lazy deallocation strategy is also available for stacks, and the difference between a single instruction needed for stack deallocation and a long procedure needed by the garbage collector is enough to ensure that for any realistic amount of available memory, stack allocation is the most efficient.
Obviously, stack allocation is not applicable to everything, which is why other techniques, like reference counts and garbage collectors, are also necessary.