Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

What is the need for garbage collection (GC) in a stack-based language? In a language like Forth or RPL (on HP calculators), is there a need for garbage collection?

I would think, since output is popped off the stack, that there wouldn't be any need. Am I missing something?

share|improve this question

2 Answers 2

up vote 11 down vote accepted

GC is normally applied to memory allocated on the heap. I'm not familiar with Forth or RPL, but if there is no heap, and everything is stored on a global stack instead, then there's nothing for GC to do.

share|improve this answer
The typical memory usage pattern of the Forth programs I read in the early days was they were like assembly programs, in that they used prearranged static global data addresses. This works because Forth has the ability to transfer data between the stack and specific addresses. Further it has the ability to compute addresses. Thus it would be possible to create an allocator library based on a heap concept and the allocator library would need some way to deal with the finite size of the heap. If a Forth user went down this path they could conceivably want and implement GC. –  cardiff space man Jul 28 at 0:11

Yes, you're right. But the stack basedness is just a part of the whole story. For example, the Java bytecode interpreter is stack-based as well (the compiled code works -- for efficiency reasons -- differently). This tells us, that any language can be transformed into a stack language.

What matters are the objects outside of the stack, those who can outlive the current method execution. As long as the language has nothing like malloc or new, there are no such objects and you need no delete nor GC.

A language lacking dynamic memory allocation is quite limited in its usefulness.

share|improve this answer
not sure I agree with the last line, is java bytecode not useful? –  jk. Sep 15 '11 at 15:46
@jk., java bytecode has dynamic memory allocation. –  Peter Taylor Sep 15 '11 at 15:50
Actually, there are several general-purpose languages that are stack based. Have a look at –  Yam Marcovic Sep 15 '11 at 15:53
Actually factor is garbage collected –  Andrea Oct 15 '13 at 8:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.