Comment by EGreg
3 hours ago
There is no need for garbage collection if you don’t form circular references. Just have a canonical direction and always keep weak references the other way.
3 hours ago
There is no need for garbage collection if you don’t form circular references. Just have a canonical direction and always keep weak references the other way.
Reference counting is a different model. Many papers have explored the differences and similarities, and your comment leaves so much out that it cannot even be said to be true or false.
I'd say GC is always the fastest to free objects within the main code path. Literally zero instructions.
> I'd say GC is always the fastest to free objects within the main code path. Literally zero instructions
True, but reference counting or free need not be far behind. They can append the pointer being freed to a per-thread list (⇒ no locking needed) that a separate thread that does the actual freeing periodically claims and then iterates over to actually free the objects.
Disadvantage is that memory usage goes up a bit because the actual freeing is delayed, but that (likely) is less so than with a garbage collector.
GC, zero instructions: that's funny; JVM used to "stop to world" to process that zero instructions;
I was explicitly talking about exactly one very specific part a way to implement GC.
It was very clear that I was not talking about the active parts of the garbage collection.
It's really cheap to produce garbage.
It doesn't leave anything relevant out.
Java pioneered this garbage collection stuff because you had cycles of references. You don't need to have cycles. WeakRef is a much better thing now. All you need is reference counting, and you don't need any garbage collection at all. When the reference count reaches 0, you destroy the object and free up its memory. It's far more predictable than GC, too.
And GC isn't "the fastest" to free objects, it has to walk a graph. The fastest is actually arena allocation and then just dropping the whole thing. But that's exactly what owning an entire container of objects can do. If you have a doubly linked list, for example, A[n] -> A[n+1] but also A[n+1] -> A[n] but neither of those should be a strong reference to prevent reclaiming. Instead, the container of that doubly linked list should be the one having a strong reference to its items.
I understood your first comment without expanding on it like this. You also don't need to explain what reference counting is.
> GC isn't "the fastest" to free objects, it has to walk a graph.
No, you don't inherently need to. And what I said is that it's super duper fast to produce garbage. I did not say that actually freeing the underlying memory, or any other cleanup, was fast.
I'm not even arguing for GCs, here. I even think GC languages tend to become tech debt generators, as garbage is not addressed until it's a really complex problem with no good solutions.