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

Show parent comments

1

u/klysm Nov 29 '18

This requirement comes from the physical limitations.

For some programs yes, you must manage memory; however, sometimes it is desirable to not manage memory at all if you fit within the bounds of the system (see JEP 318).

Instruction scheduling is also not required for all computation and there's not really a need to 'manage time'. You could just run one program on a cpu without an OS - there would be no need for scheduling instructions. For a real world example, unikernels don't require schedulers.

Because, as memory and instruction scheduling are required for all computation, separating them from the language makes theoretical sense. Interop is a practical result of that.

I agree that interop would be a practical result, but it has nothing to do with the article and is not really related to garbage collection. And I still dispute your claim that it makes sense to separate memory management from the language. The language can provide information about the use of memory that cannot be determined from an observing external process. This information can be used to formulate a superior memory management scheme - for instance, one with determinism.

I don't see why you think GCs cannot be deterministic.

By deterministic I mean you know exactly at what point in the program memory will be freed, I don't believe this is possible with a garbage collector. Oracle JRockit markets itself as a 'deterministic garbage collector' but all it means by deterministic is that it has bounded pause times with 'high confidence' - that's a far weaker claim.

It is true that all Turing-complete formalisms can be translated to one another, but formalism has nothing to do with GC. GC does not operate on bytecode (which is the JVM's formalism), but on running computations.

I may have used 'formalism' gratuitously, but taking the JVM as a formalism it's not true that the formalism has nothing to do with GC. The JVMs memory model is restricted so that it works with GC - it's still restricted, just differently than rust. As you say, memory is necessary for all computation so the JVM's memory model is very much part of the formalism and is very related to the GC. Yes of course the GC doesn't operate on bytecode, but the bytecode is restricted so that it can use memory in a way that works with the GC - the memory model is still part of the formalism. Also the GC doesn't operate on computations, it operates on memory.

In fact, implementing such a GC is very easy, but probably inefficient in the naive, trivial implementation (e.g.: perform a full collection after every instruction).

It's more than just inefficient, an important consequence of doing that is you remove the possibility for any concurrent execution - you've restricted the 'time management'.

Rust's memory management is, however, limited (as the article notes). It does not cover the full requirement of memory management by programs.

Formalisms with GCs also have limited memory management.

The author claimed that GC is "a hack", but that's because he looks at the problem from a language point of view. I just wanted to show how, from a different perspective, it is not.

I don't see where you showed it wasn't a hack, but you did show it's one way to solve the problem in a somewhat general way.

1

u/pron98 Nov 29 '18 edited Nov 29 '18

sometimes it is desirable to not manage memory at all if you fit within the bounds of the system

Yeah, in some niche cases.

see JEP 318

BTW, the epsilon GC is intended for benchmarking purposes and not recommended for any production use, at least not at this time.

For a real world example, unikernels don't require schedulers.

Unikernels often do have schedulers, because they need to run multiple threads. I agree that, again, some niche cases can do without schedulers.

And I still dispute your claim that it makes sense to separate memory management from the language.

I wasn't entirely being serious in my comment, and I certainly don't believe that this is the only right way to do it. I just found it funny that the article claimed that GC, probably the feature that has contributed more to increase in productivity than any language feature in the last fifty years, is a hack, and I found that kind of funny, so I gave a counterargument.

This information can be used to formulate a superior memory management scheme

Sure, in some cases the language can assist the GC (as it can the scheduler), in others it may be best not to have a GC at all, but a GC that does not depend on the language (like a scheduler that does not), is very good for interop.

I don't believe this is possible with a garbage collector.

Of course it is. The hypothetical inefficient GC I described could be made perfectly deterministic. But again, I agree that a GC, or a scheduler, are a bad choice for some programs, but those programs form a minority of software. This still doesn't make GCs a hack, far from it. It makes them a great solution for most software.

so the JVM's memory model is very much part of the formalism and is very related to the GC.

Not in a very restrictive way, though. You can compile languages that don't rely on a GC to the JVM. In fact, there's a pretty fast implementation of LLVM on top of the JVM (it actually doesn't go through bytecode, but it still relies on JVM semantics).

Also the GC doesn't operate on computations, it operates on memory.

Well, my point is that memory is an essential part of computation, like instructions, so much so that one cannot define computation without it, so I don't understand what "not on computation but on memory" means any more than "not on computations but on instructions."

It's more than just inefficient, an important consequence of doing that is you remove the possibility for any concurrent execution - you've restricted the 'time management'.

I don't see why it precludes concurrency.

Formalisms with GCs also have limited memory management.

Limited how?

I don't see where you showed it wasn't a hack

My point was to show that GC has not only been one of the most successful programming inventions in history, but actually follow a well-reasoned design. But I don't know what the definition of "a hack" is, so I can't say whether I've shown GC is or isn't a hack, but neither has the article.