How common are circular references? Would reference-counting GC work just fine

garbage-collectionlanguage-design

How common are circular references? The less common they are, the fewer hard cases you have if you are writing in a language with only reference counting-GC. Are there any cases where it wouldn't work well to make one of the references a "weak" reference so that reference counting still works?

It seems like you should be able to have a language only use reference counting and weak references and have things work just fine most of the time, with the goal of efficiency. You could also have tools to help you detect memory leaks caused by circular references. Thoughts, anyone?

It seems that Python uses references counting (I don't know if it uses a tracing collector occasionally or not for sure) and I know that Vala uses reference counting with weak references; I know that it's been done before, but how well would it work?

Best Answer

Weak references work great for breaking loops, as long as it's clear a loop is being made and as long as the developer makes provision for it with a weak reference. This isn't the same as a more robust GC, which is intended to handle such things automatically so the developer doesn't have to think about them at all.