Reference Counting - Use in Garbage Collection

Use in Garbage Collection

As a garbage collection algorithm, reference counting tracks, for each object, a count of the number of references to it held by other objects. If an object's reference count reaches zero, the object has become inaccessible, and can be destroyed.

When an object is destroyed, any objects referenced by that object also have their reference counts decreased. Because of this, removing a single reference can potentially lead to a large number of objects being freed. A common modification allows reference counting to be made incremental: instead of destroying an object as soon as its reference count becomes zero, it is added to a list of unreferenced objects, and periodically (or as needed) one or more items from this list are destroyed.

Simple reference counts require frequent updates. Whenever a reference is destroyed or overwritten, the reference count of the object it references is decremented, and whenever one is created or copied, the reference count of the object it references is incremented.

Reference counting is also used in disk operating systems and distributed systems, where full non-incremental tracing garbage collection is too time consuming because of the size of the object graph and slow access speed.

Read more about this topic:  Reference Counting

Famous quotes containing the words garbage and/or collection:

    To be an editor, as I was.
    Then to lie here close by the river over the place
    Where the sewage flows from the village,
    And the empty cans and garbage are dumped,
    And abortions are hidden.
    Edgar Lee Masters (1869–1950)

    All urbanization, pushed beyond a certain point, automatically becomes suburbanization.... Every great city is just a collection of suburbs. Its inhabitants ... do not live in their city; they merely inhabit it.
    Aldous Huxley (1894–1963)