4 ms·
The Garbage Collection Handbook [http://gchandbook.org/ http://gchandbook.org/] has a good overview of the advantages and disadvantages of reference counting as
by adrianm 10y ago
The Garbage Collection Handbook [http://gchandbook.org/ http://gchandbook.org/] has a good overview of the advantages and disadvantages of reference counting as garbage collection.
In summary,
Advantages:
A. Objects are collected immediately when they become garbage.
B. Programming language runtime support is simpler.
C. Memory can be reclaimed easily in programs with distributed heaps.
Disadvantages:
A. Natural race conditions exist in multi-threaded programs when reference counting is used. Atomic read/writes are necessary when incrementing/decrementing the reference count for an object.
B. Cyclic data structures cannot be reclaimed. (This is why you have to jump through hoops to avoid this in Objective-C and Swift.)
C. Every object in your language which is reference counted needs a field to hold the reference count. Or you need a special object in its stead which "points" to your object, like a shared pointer.
D. Pauses with reference counting are still possible. As stated in the book, "When the last reference to the head of a large pointer structure is deleted, reference counting must recursively delete the descendant of the root."
- pjmlp 10y agoRegarding D, you can even get stack overflows if the destructors require nested calls.
- masklinn 10y ago> C. Every object in your language which is reference counted needs a field to hold the reference count. Or you need a special object in its stead which "points" to your object, like a shared pointer. For a long time (until the 64b class pointers I think) Objective-C used external tables of reference counts instead of embedding refcounts in objects.
- mayoff 10y agoThe side table only gets a slot for an object when the object is retained (has its reference count incremented) after creation. (Every object starts with a count of 1 at creation.) Before ARC, you could certainly have lots of objects that were never retained after creation. Since ARC tends to be more careful than a human about retaining objects (meaning it retains objects in more places), I don't know how much more load ARC puts on the side table, but I suspect it's not zero.
- ridiculous_fish 10y agoTwo more advantages which are relevant for Swift: 1. Memory high-water-mark is generally lower with RC, which is important for mobile. 2. RC lets you easily determine when there is only one reference to an object. This enables Swift's collections to be value types, one of Swift's key semantics.
- masklinn 10y ago> 2. RC lets you easily determine when there is only one reference to an object. This enables Swift's collections to be value types, one of Swift's key semantics. It also enables some optimisations e.g. Python has immutable strings, but CPython can take advantage of rc=1 to extend strings in-place (avoiding accidentally quadratic concatenation loops until you start storing intermediate references or you try to run the software on alternate implementations)
- deleted 10y ago[deleted]
- cpeterso 10y agore ref counting pauses from cascading frees, you can lazily free big data structures by deferring to a later event loop tick or idle event.
- chrislattner 10y agoAnother major advantage: No need for "finalizers" which can often run on an unpredictable thread, resurrect objects, etc. There is extensive literature about how problematic finalizers are in practice. deinit in Swift suffers from none of these problems.
- aardvark179 10y agoThere are better alternatives to finalisers for GC'ed languages. Phantom refs in Java are a good example of a finalisation mechanism that explicitly prevents resurrection of objects.