← Back to context

Comment by einpoklum

3 years ago

I feel the need for garbage collection is a language design mis-feature. That is to say, producing garbage is a language design-mis-feature. To quote Bjarne Stroustrup:

> I don't like garbage. I don't like littering. My ideal is to eliminate the > need for a garbage collector by not producing any garbage. That is now > possible.

and it's indeed possible. For example It's become pretty much a non-issue in modern C++: https://stackoverflow.com/a/48046118/1593077 (and C++ is not the only example, it's just a prominent example of a language which almost standardized garbage collection, but eventually did not go that way.)

C++ can actually produce quite a bit of garbage unintentionally, it's why linters will remind you often to call std::move.

That said I much prefer deterministic resource cleanup even in a janky language like C++ over a tracing GC.

  • It's true that C++ can trigger a lot of unnecessary copying if one writes carelessly; but that's not the same as garbage, in that both copies are used, and none of them continue living indefinitely without special intervention. But point taken about pass-by-value deficiencies.

You can’t avoid garbage if you deal with generic programs — Rust and C++ can only implement a subset of all programs without RC (which is the most basic GC algorithm).

This is the same way to many other interesting CS properties — most of them are undecidable at compile time, so you have to do it at runtime.

More and more people nowadays are programming at high levels of abstraction. If you're designing the frontend of a website, or making a mobile game, or developing a stock trading algorithm, or whatever else, then you probably don't want to constantly worry about details of memory management...

On top of this, GC is necessary for some algorithms. Any data structure with partial sharing (e.g. binary search tree with versioning via path-copying) needs GC to be space-efficient. You could either rely on a built-in GC, or write your own. If you write your own, I think you'll find that it is tedious and error-prone due to memory safety issues.

  • We could already be doing that during the 1980's had it not been for UNIX getting out of Bell Labs with source tapes, and bad management decisions.

    https://interlisp.org/

    http://toastytech.com/guis/cedar.html

    "Eric Bier Demonstrates Cedar"

    https://www.youtube.com/watch?v=z_dt7NG38V4&t=2s

    "Making Smalltalk"

    https://www.youtube.com/watch?v=PaOMiNku1_M

    http://www.edm2.com/index.php/VisualAge_Smalltalk

    Also there is to note that most BASIC implementations had support for automatic memory management, at least for strings and arrays, the structured compiled dialects even better.

    Also database programming with languages like Clipper and FoxPro.

    And even if we stay in the UNIX world, that is exactly using stuff like Perl also allowed for, C like programming without the headaches of manual memory management.

    Or the brief fad of 4GL languages.

  • > then you probably don't want to constantly worry about details of memory management...

    Oh, you actually don't have to, that's the whole point... in the past, you (effectively) had a choice between careful manual management of memory and garbage collection with its overheads. These days, you can use constructs which take care of that management for you, with very little or no overhead, which don't involve garbage.

    It's true that sometimes GC is algorithmically necessary; but then the high-level-of-abstraction argument is irrelevant. And in those cases, a GC library does indeed come in useful. You don't need to write your own, others have likely done it already.