3 ms·
Reference counting doesn't work well with circular references or multiple threads. It also requires an extra field for every object. There are ways around part
by TheLoneWolfling 13y ago
Reference counting doesn't work well with circular references or multiple threads. It also requires an extra field for every object.
There are ways around parts of this, but most people just moved to mark and sweep instead.
(One can use thread-local counters and a global map of what threads have at least one reference to an object, updated only when a reference goes from 0 to 1 or vice versa. Weighted reference counting is an alternative. There are also a couple ways of detecting circular references.)
- pat_shaughnessy 13y agoI discuss circular references and how CPython handles them a bit in my followup post: http://patshaughnessy.net/2013/10/30/generational-gc-in-python-and-ruby http://patshaughnessy.net/2013/10/30/generational-gc-in-pyth...
- lojack 13y agoThe author's second article also contains information on how Python deals with circular references (a tracking garbage collector very similar to mark-and-sweep). http://patshaughnessy.net/2013/10/30/generational-gc-in-python-and-ruby http://patshaughnessy.net/2013/10/30/generational-gc-in-pyth... It's also worth noting that you don't always need to deal with circular references in the case of short running programs. Additionally you can eliminate the possibility of ever creating a circular reference by using an immutable language.
- thinkpad20 13y agoSpeaking of immutable languages, there are actually tricks in Haskell by which it can actually generate circular references because of its laziness (ML, on the other hand, is immutable and strict, so it cannot, by default anyway). In regards to reference counting vs. garbage collection, one advantage to reference counting is that it can be more memory efficient, since objects do not unnecessarily accumulate in memory. It can be advantageous in embedded systems for this reason. GC can provide a speed boost at a memory usage cost, because `malloc` and `free` are relatively expensive, and using a garbage collector can reduce the number of these calls needed.