Reference counting

Under reference counting (English reference counting ) is understood in programming a technique to manage the number of references (references or pointers) to a particular object. The objective is to detect when an object is no longer needed and can be deleted. Reference counting is a possibility of automatic garbage collection.

Implementation

To implement such a reference counting, it is necessary to manage each object a reference count. It must also be ensured that this counter is incremented each additional referencing in establishing and decremented upon completion of a referencing.

With consistent implementation of such an approach two conditions are always satisfied:

A problem with this technique are cyclic references, that is, two or more objects that reference each other. If it objects refer only to treat each other and there is no reference from the outside longer exists, that is, from other objects outside of the cycle, the respective reference counters are not 0 and it is not so readily apparent that these objects are not actually no more needed. If such a situation can not be excluded in principle, exist to detect such a situation, different algorithms, most of which are based in the graph on the principle of accessibility.

Application Examples

Programming environments that use reference counting:

  • Component Object Model
  • Cocoa
  • Object Pascal (for Strings)
  • GObject / Vala
  • Perl 5
  • PHP
  • Python
  • Squirrel

In programming environments without such automatic memory management, such as C or C , it may be necessary to implement such a reference counting itself, if the order of release of objects is not implemented in a predictable and well-defined sequence with obvious hierarchical dependencies. In object-oriented languages ​​such as C , it is appropriate here to encapsulate such a method by a class. A typical example is a String class, which ensures that similar content will be created in memory only once and yet ensures that unneeded content will be deleted without the programmer must explicitly worry about it.

Pictures of Reference counting

91424
de