Tell me more ×
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.

This may be a stupid question but I cannot figure out the need for Garbage Collection 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 would be no need. Am I missing anything?

share|improve this question

2 Answers

up vote 9 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 Java bytecode interpreter is stack based as well (the compiled code works -- for efficiency reasons -- differently). This says 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
1  
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

Your Answer

 
discard

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.