Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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

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 factorcode.org –  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.