What are the disadvantages of the reference counting method when detecting garbage?

    1 answer 1

    Reference counting 

    The essence of the approach is that each object has a counter. The counter stores information about how many links points to an object. When the link is destroyed, the counter is decremented. If the value of the counter is zero, the object can be considered garbage and the memory can be cleared.

    The main disadvantage of this approach is the difficulty of ensuring the accuracy of the counter. Also with this approach it is difficult to identify cyclical dependencies (when two objects point at each other, but not a single living object refers to them). This leads to memory leaks.

    • What does the wording mean: "but no living object refers to them"? - bsuart
    • @bsuart SomeClass a = new SomeClass(); SomeClass b = a; a = null; SomeClass a = new SomeClass(); SomeClass b = a; a = null; Those. there were two references to one instance of the class, then one was annulled, and the second also became equal to null. This behavior is very difficult to track down - I. Perevoz