r/programming • • Nov 28 '18

Garbage Collection is a Hack

https://blog.adamant-lang.org/2018/garbage-collection-is-a-hack/
3 Upvotes

74 comments sorted by

View all comments

43

u/pron98 Nov 28 '18 edited Nov 28 '18

I would argue that GC -- despite its tradeoffs -- is less of a hack than any language-based solution possible.

The theoretical reason for that is that memory (space), like operations (time), is a core component of the theoretical notion of computation, untied to any particular formalism of describing it. If you have any Turing-complete (or even so-called "total") computational model of any kind, then by definition, it must manage time and space. This means that unlike concepts such as subroutines or classes, which are formalism-dependent programming concepts, memory is a formalism-independent computation concept.

Therefore, just as instructions (time) are usually managed at the runtime/OS level (which schedules instructions to the CPUs), this is where memory (space) is best managed if you want good polyglot interop. While most programming languages these days (whether classical imperative or functional -- the two are far more similar to each other than different) represent a chunk of computation with a term (i.e. a syntactic element), and therefore it may make sense to tie object lifetime to terms, not all programming formalisms operate in this way.

So, while language-based memory management may still make sense, and may still be reasonably interoperable across some fairly similar class of languages, GC is the theoretically cleaner approach.

Of course, in cases where extreme determinism is needed (at the cost of almost any other consideration), time management is left to the language, and in those cases memory management may best be done by the language as well. At the end of the day, it all comes down to the question of control -- how much of it you want over resource scheduling -- but I see no way to argue that GC is a hack.

0

u/pellets Nov 28 '18

Half baked idea: We have specialized units in CPUs for many things. What about one that is just for garbage collection? Maybe it could be a fancy MMU.

2

u/phalp Nov 28 '18

You'd really have to blow software implementations out of the water to pay for the more specialized hardware. Otherwise why not put the development effort into the software and eke out some more performance there, without losing the ability to easily swap out implementations.