I
Insight Horizon Media

Why is reference counting bad?

Author

Rachel Hickman

Published Mar 20, 2026

Why is reference counting bad?

Reference counting only involves objects involved in an assignment; it’s cost is not related to the total memory used by an application. Freeing an object during reference counting could however have a cascading effect. The local release of an object could result in a large tree of objects being released.

How can we maintain the reference count of a pointer?

All smart pointers that refer to the same object must share a single reference count. The object holding the reference count is allocated by the first smart pointer object, and pointers to it (NOT the object itself) are copied when the smart pointer is copied.

Is reference counting faster than garbage collection?

Tracing GC is not always faster. GC time is proportional to the size of the allocated memory, reference counting time is not. Reference counting time is proportional to data structure sizes on deletion.

What is a major drawback of reference counting garbage collection?

Cyclic Structures: As we discussed earlier, the biggest drawback of reference counting is its inability to reclaim cyclic storage. Under simple reference counting approach, cyclic data-structures like doubly linked lists or non-simple graphs cannot be efficiently reclaimed and will leak memory.

What languages use reference counting?

C++ and Rust both provide reference counting pointer-like-things (shared_ptr and Rc respectively). C++ has been around for quite a while, of course, but Rust is pretty new, and deliberately eschewed garbage collection.

Does Ruby use reference counting?

Ruby uses John McCarthy’s original mark and sweep algorithm, while Python uses reference counting. But when we look more closely, we see that Python uses bits of the mark and sweep idea to handle cyclic references, and that both Ruby and Python use generational garbage collection in similar ways.

How do you do reference counting?

Reference counting is one such technique. This method is simply keeping an extra counter along with each object that is created. The counter is the number of references that exist to the object, in the C/C++ case this would how many pointers refer to this object.

How do you implement reference counting?

In each object, keep a count of the number of references to the object. (Add 1 when copying the reference, subtract 1 when clearing or changing a reference.) When the count drops to zero, kill the object.

What are the advantages of reference counting vs GC?

The main advantage of the reference counting over tracing garbage collection is that objects are reclaimed as soon as they can no longer be referenced, and in an incremental fashion, without long pauses for collection cycles and with clearly defined lifetime of every object.

Is reference counting a memory management technique?

Reference counting systems perform automatic memory management by keeping a count in each object, usually in a header, of how many references there are to the object. Objects to which there are no references cannot be accessed by the mutator; they are therefore dead and may be reclaimed.

How is garbage collection done using reference count?

Reference counting garbage collection is where each object has a count of the number of references to it. Garbage is identified by having a reference count of zero. An object’s reference count is incremented when a reference to it is created, and decremented when a reference is destroyed.

Does Python use mark and sweep?

Ruby uses John McCarthy’s original mark and sweep algorithm, while Python uses reference counting.