16

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?

4 Answers 4

14

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.

1
  • 2
    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. Jul 28, 2015 at 0:11
13

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.

4
  • not sure I agree with the last line, is java bytecode not useful?
    – jk.
    Sep 15, 2011 at 15:46
  • @jk., java bytecode has dynamic memory allocation. Sep 15, 2011 at 15:50
  • 1
    Actually, there are several general-purpose languages that are stack based. Have a look at factorcode.org Sep 15, 2011 at 15:53
  • 1
    Actually factor is garbage collected
    – Andrea
    Oct 15, 2013 at 8:33
1

Garbage collection is required if the language is going to support dynamic data structures inherently. Which is almost a necessity if you want to do anything beyond the level of C. Without it you are stuck with only fixed sized data structures and managing memory yourself. That's what original Forth does of course, but it's probably not something you'd want to do today unless your are only doing low-level systems coding.

1

Garbage collection is not needed if the language uses static allocation instead of stack allocation. For example Fortran 77 with the -s (static storage) option allocates all memory when the program starts, therefore no memory allocation occurs at runtime to be freed. While it takes some discipline, it is possible to write programs, especially simulations to use static memory allocation. Static allocation removes any memory leaks, and leads to terrific cache performance since the compiler can use static analysis to determine what to load into cache.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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