Weak reference

Weak reference

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.

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.

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).

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.

External links

* [http://www.pawlan.com/Monica/refobjs/ Java developer article: 'Reference Objects and Garbage Collection']
* [http://weblogs.java.net/blog/enicholas/archive/2006/05/understanding_w.html Understanding Weak References]
* [http://rcache.sourceforge.net/ RCache - Java Library for weak/soft reference based cache]
* [http://mindtrove.info/articles/python-weak-references/ Python Weak References]


Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать реферат

Look at other dictionaries:

  • Reference counting — In computer science, reference counting is a technique of storing the number of references, pointers, or handles to a resource such as an object or block of memory. It is typically used as a means of deallocating objects which are no longer… …   Wikipedia

  • Reference (computer science) — This article is about a general notion of reference in computing. For the more specific notion of reference used in C++, see Reference (C++). In computer science, a reference is a value that enables a program to indirectly access a particular… …   Wikipedia

  • Weak symbol — In computing, a weak symbol is a symbol definition in an object file or dynamic library that may be overridden by other symbol definitions. See also * Link editor * Program loader * Dynamic linking * Weak reference …   Wikipedia

  • Reference ranges for blood tests — Reference ranges edit in: blood urine CSF feces Reference ranges for blood tests are sets of values used by a health professional to interpret a set of …   Wikipedia

  • Weak Become Heroes — Infobox Single Name = Weak Becomes Heroes Artist = The Streets from Album = Original Pirate Material Released = July 22 2002 Format = CD [ Recorded = ] Genre = Electronica, UK garage Length = 05:33 Label = 679 [ Writer = Mike Skinner] Producer =… …   Wikipedia

  • Weak base — In chemistry, a weak base is a chemical base that does not ionize fully in an aqueous solution. As Bronsted Lowry bases are proton acceptors, a weak base may also be defined as a chemical base in which protonation is incomplete. This results in a …   Wikipedia

  • Weak central coherence theory — The weak central coherence theory (WCC), also called the Central coherence theory (CC), suggests that a specific perceptual cognitive style, loosely described as a limited ability to understand context or to see the big picture , underlies the… …   Wikipedia

  • weak password —    A password that is easy to guess, such as an English word, any reference to Star Trek or Star Wars, or a word or number that relates to the user, such as the name of a family member or a birth date.    See also password; strong password …   Dictionary of networking

  • Soft reference — A soft reference is one of the strengths or levels of non strong reference defined in the Java programming language, the others being weak and phantom.The garbage collector will always collect weakly referenced objects, but will only collect… …   Wikipedia

  • Meta-reference — Metareference, a metafiction technique, is a situation in a work of fiction whereby characters display an awareness that they are in such a work, such as a film, television show or book. Sometimes it may even just be a form of editing or film… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”