Weak Reference - Garbage Collection

Garbage Collection

Garbage collection is used to clean up unused objects and so reduce the potential for memory leaks and data corruption. There are two main types of garbage collection: tracing and reference counting. Reference counting schemes record the number of references to a given object and collect the object when the reference count becomes zero. Reference-counting cannot collect cyclic (or circular) references because only one object may be collected at a time. Groups of mutually referencing objects which are not directly referenced by other objects and are unreachable can thus become permanently resident; if an application continually generates such unreachable groups of unreachable objects this will have the effect of a memory leak. Weak references (references which are not counted in reference counting) may be used to solve the problem of circular references if the reference cycles are avoided by using weak references for some of the references within the group. For example, Apple's Cocoa framework recommends this approach, by using strong references for parent-to-child references, and weak references for child-to-parent references, thus avoiding cycles.

Weak references are also used to minimize the number of unnecessary objects in memory by allowing the program to indicate which objects are not critical by only weakly referencing them.

Read more about this topic:  Weak Reference

Famous quotes containing the words garbage and/or collection:

    A mental disease has swept the planet: banalization.... Presented with the alternative of love or a garbage disposal unit, young people of all countries have chosen the garbage disposal unit.
    Ivan Chtcheglov (b. 1934)

    Bolkenstein, a Minister, was speaking on the Dutch programme from London, and he said that they ought to make a collection of diaries and letters after the war. Of course, they all made a rush at my diary immediately. Just imagine how interesting it would be if I were to publish a romance of the “Secret Annexe.” The title alone would be enough to make people think it was a detective story.
    Anne Frank (1929–1945)