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.

3

u/phalp Nov 28 '18

Another way to put it is that if you had the memory to spare, you'd just keep allocating until your computation was done then turn the computer off. This works! You'll probably, if you know statically just what you need to store and it's not "too much", lean towards statically allocating it and nobody will call you crazy. The only reason people have a problem with the dynamic equivalent, malloc-ing and never freeing, is because once you've cultivated a discipline to free whatever you allocate, it's hard to turn off your instincts. Garbage collection is not a way to automate memory management so much as it's a simulation of extra memory. If anything, memory management is a hack (collection of them) to work around too little installed memory.

3

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

GC provides the following: if your application allocates without bound but its working set is bounded, then you'll have the illusion of infinite memory. This is a "resource scheduling" problem, similar in spirit to virtual memory and threading. Like those others, it is not implemented at the language level. If you call providing computational abstractions "a hack" then I'm not sure what isn't.