4 ms·
> A pretty naive question: what is the advantage of garbage collection over just reference counting? It's not that naive, and the unpopular answer is "not much
by ajross 4y ago
> A pretty naive question: what is the advantage of garbage collection over just reference counting?
It's not that naive, and the unpopular answer is "not much". A good modern GC, faced with a usage paradigm that it expects (this part is important!), is almost certainly going to be faster. It's indeed safe from circular reference leaks. Generational collectors can often compact memory better and have a smaller footprint for the same data.
Those are all real advantages. But they come at the cost of outrageous complexity and code footprint. A reference counter is probably more than an 80% solution for almost any reasonable application, and it's something college kids write in 15-20 lines in their intro course.
- astrange 4y agoReference counting also usually has lower peak memory and scans less memory so is friendlier when you have either battery power or swap, ie, any non-server use case.
- rwmj 4y agoPractically, someone else writes the garbage collector, so the supposedly "outrageous" complexity isn't an actual issue. Also I wouldn't say the complexity is much different from any other issue writing a programming language.
- ajross 4y agoThat's true for the case of simple apps that use a single runtime. Lots and lots of work is done "in Java" or "in Go". Lots more, including most of the largest and most complicated[1] apps, require synthesis of lots of tools from lots of sources. Those apps need to work very hard to get their heaps to play nicely with each other, and the tuning required by modern collected runtimes tends to blow up in strange ways. [1] And especially including the ones targetted by the Boehm collector in the linked github. Also apps for which Rust (which famously eschews a collected heap) tends to be proposed.
- kaba0 4y agoA very naive tracing GC is not more complex either, and is absolutely in the ballpark of “college kids”.
- ajross 4y agoYes, but a world-stopping mark and sweep collector is pretty much a non-starter for anything non-trivial in the modern world with full heaps well into the gigabytes. You'd be talking about multiple seconds of delay.