Why doesn D use reference counting for garbage collection?
Reference counting has its advantages, but some severe disadvantages: • Cyclical data structures won’t get freed. • Every pointer copy requires an increment and a corresponding decrement – including when simply passing a reference to a function. • In a multithreaded app, the incs and decs must be synchronized. • Exception handlers (finally blocks) must be inserted to handle all the decs so there are no leaks. Contrary to assertions otherwise, there is no such thing as “zero overhead exceptions.” • In order to support slicing and interior pointers, as well as supporting reference counting on arbitrary allocations of non-object data, a separate “wrapper” object must be allocated for each allocation to be ref counted. This essentially doubles the number of allocations needed. • The wrapper object will mean that all pointers will need to be double-dereferenced to access the data. • Fixing the compiler to hide all this stuff from the programmer will make it difficult to interface cleanly wi