Weak reference

From Wikipedia, the free encyclopedia

In computer programming, a weak reference is a reference that does not protect the referent object from collection by a garbage collector. An object referenced only by weak references is considered unreachable (or "weakly reachable") and so may be collected at any time. Weak references are used to prevent circular references and to avoid keeping in memory referenced by unneeded objects. Many garbage-collected, object-oriented languages feature or support weak references, such as Java, C#, Python, Perl, Lua, REALbasic, and ActionScript 3.0.

Contents

[edit] Garbage collection

Garbage collection is used to 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 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.

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.

[edit] Various implementations

Some languages have multiple levels of weak reference strength. For example, Java has — in order of decreasing strength — soft, weak, and phantom references, defined in the package java.lang.ref.

Some non-garbage-collected languages, such as C++, provide weak/strong reference functionality as part of supporting garbage collection libraries. In the case of C++, normal pointers are "weak" and smart pointers are "strong" (although pointers are not true weak references, as weak references are supposed to know when the object becomes unreachable).

[edit] Examples

Weak references can be useful in keeping track of the current variables being referenced in the application. This list must have weak links to the objects. Otherwise, once objects are added to the list, they will be referenced by it and will persist forever (or until the program stops).

Another use of weak references is in writing a cache. Using, for example, a weak hash map, one can store in the cache the various referred objects via a weak reference. When the garbage collector runs — when for example the application's memory usage gets sufficiently high — those cached objects which are no longer directly referenced by other objects are removed from the cache.

[edit] External links

Languages