News for hackers. Stuff that matters.
Re:Modern garbage collection: Part 2 by LessDmesg on Saturday December 14, 2019 @02:41PM (#21791663)
Reference counting is not better, it's a poor man's GC. Reference counting means: 1) instruction pollution from all the refcount updates and their synchronization 2) circular references, ie memory leaks 3) too much time spent freeing memory (whereas a generational GC spends time on live objects only) 4) memory fragmentation and hence slow allocation (whereas in a normal GC allocation is just O(1)
Re:Modern garbage collection: Part 2 by w0utert on Sunday December 15, 2019 @08:15AM (#21796063)
Languages like Swift and Objective-C hide the reference counting for you, so 'instruction pollution' is not really something I care about. The overhead at the instruction & memory level is fairly minimal for these language too, by means of using tagged pointers. I'm pretty sure the compiler is smart enough to factor out reference counting for complete sections where it can determine objects can impossibly go out of scope (e.g. sections where aliasing can be ruled out, and all assignments are to locals, such as in many loops).

Circular references can be a problem, this is just something you have to live with and design for, just as in languages with manual memory management. In the typical cases where this can be a problem (graphs of objects) its very straightforward how to fix them using weak references.

I don't understand point 3 and 4 and why they would be a property of reference counting for memory management. They both seem completely orthogonal problems that have nothing to do with the mechanisms that decide when to free memory.

Anyway, my original point was not that reference counting is perfect, or even more efficient compared to garbage collection. Just that it is predictable and deterministic, which is very often much more important, especially for code with real-time constraints.

Re:Modern garbage collection: Part 2 by liuliu on Monday December 16, 2019 @04:56PM (#21807742)
> Languages like Swift and Objective-C hide the reference counting for you, so 'instruction pollution' is not really something I care about. The overhead at the instruction & memory level is fairly minimal for these language too, by means of using tagged pointers. I'm pretty sure the compiler is smart enough to factor out reference counting for complete sections where it can determine objects can impossibly go out of scope (e.g. sections where aliasing can be ruled out, and all assignments are to locals, such as in many loops).

New Swift Ownership API can help, but compiler is not that good to figure out exclusive ownership all by themselves without any annotation. It is common to have more than 10% of you time in RC environment spent on refcount calculations and locks acquisition (some stats: http://iacoma.cs.uiuc.edu/iacoma-papers/pact18.pdf).

Re:Modern garbage collection: Part 2 by zozbot234 on Sunday December 15, 2019 @08:22AM (#21796094)
> instruction pollution from all the refcount updates and their synchronization

If the language supports ownership tracking and non-synchronized Rc<> as in Rust, refcount updates ought to be rare and/or quick. I agree that this is very much an issue in languages with obligate "managed" memory such as Swift, and that tracing GC may sometimes be preferable to that.

> too much time spent freeing memory

If you're using Rc, you probably care about memory reclaim being deterministic, which tracing GC doesn't give you. You can also use arenas to free a bunch of objects all at once; this also addresses memory fragmentation and slow allocation to some extent.

Re:Modern garbage collection: Part 2 by saagarjha on Saturday December 14, 2019 @06:18PM (#21793073)
> circular references

You can solve this with weak references.

Re:Modern garbage collection: Part 2 by LessDmesg on Sunday December 15, 2019 @03:27AM (#21795145)
...which is manual work with no guarantees of correctness.
Re:Modern garbage collection: Part 2 by ncmncm on Monday December 16, 2019 @12:00AM (#21800911)
But never, in practice, needed, so any downside is only theoretical.