Content deleted Content added
Meatsgains (talk | contribs) Filled in 4 bare reference(s) with reFill () |
|||
Line 1:
In [[computer programming]], a '''weak reference''' is a [[reference (computer science)|reference]] that does not protect the referenced [[object (computer science)|object]] from collection by a [[garbage collection (computer science)|garbage collector]], unlike a strong reference. An object referenced ''only'' by weak references – meaning "every chain of references that reaches the object includes at least one weak reference as a link" – is considered ''[[weakly reachable]],'' and can be treated as [[unreachable memory|unreachable]] and so may be collected at any time. Some garbage-collected languages feature or support various levels of weak references, such as [[C Sharp (programming language)|C#]], [[Java (programming language)|Java]], [[Lisp (programming language)|Lisp]], [[OCaml]], [[Perl]], and [[Python (programming language)|Python]].<ref>[https://docs.python.org/3/library/weakref.html 8.8. weakref — Weak references], The Python Standard Library</ref>
Line 9 ⟶ 8:
Garbage collection is used to clean up unused objects and so reduce the potential for [[memory leak]]s 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.
A very common case of such strong vs. weak reference distinctions is in tree structures, such as the [[Document Object Model]] (DOM), where parent-to-child references are strong, but child-to-parent references are weak. For example, Apple's [[Cocoa (API)|Cocoa]] framework recommends this approach.<ref>
Weak references are also used to minimize the number of unnecessary objects in memory by allowing the program to indicate which objects are of minor importance by only weakly referencing them.
Line 26 ⟶ 25:
===Java===
Java 1.2 in 1998 introduced<ref>{{cite web|url=http://docs.oracle.com/javase/7/docs/api/java/lang/ref/WeakReference.html|title=WeakReference (Java Platform SE 7 )|website=docs.oracle.com}}</ref> two kinds of weak references, one known as a “soft reference” (intended to be used for maintaining GC-managed in-memory caches, but which doesn’t work very well in practice<ref>{{cite web|url=https://developer.android.com/reference/java/lang/ref/SoftReference.html|title=SoftReference - Android Developers|website=developer.android.com}}</ref>) and the other simply as a “weak reference”. It also added a related experimental mechanism dubbed “phantom references” as an alternative to the dangerous and inefficient finalize() mechanism.<ref>{{cite web|url=http://docs.oracle.com/javase/7/docs/api/java/lang/ref/PhantomReference.html|title=PhantomReference (Java Platform SE 7 )|website=docs.oracle.com}}</ref>
If a weak reference is created, and then elsewhere in the code <code>get()</code> is used to get the actual object, the weak reference isn't strong enough to prevent garbage collection, so it may be (if there are no strong references to the object) that <code>get()</code> suddenly starts returning null.<ref>http://weblogs.java.net/blog/2006/05/04/understanding-weak-references Java Examples</ref>
|