Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Weak reference
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|In programming, a reference which does not protect its object from garbage collection}} 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#]], [[Lua (programming language)|Lua]], [[Java (programming language)|Java]], [[Lisp (programming language)|Lisp]], [[OCaml]], [[MATLAB]],<ref>[https://uk.mathworks.com/help/matlab/matlab_oop/weak-reference-handles.html]</ref> [[Perl]], [[Python (programming language)|Python]]<ref>[https://docs.python.org/3/library/weakref.html 8.8. weakref β Weak references], The Python Standard Library</ref> and [[PHP]] since the version 7.4.<ref>{{Cite web|url=https://www.php.net/manual/en/class.weakreference.php|title = PHP: WeakReference - Manual}}</ref> ==Uses== Weak references have a number of common uses. When using [[reference counting]] garbage collection, weak references can break [[reference cycle]]s, by using a weak reference for a link in the cycle. When one has an [[associative array]] (mapping, hash map) whose keys are (references to) objects, for example to hold auxiliary data about objects, using weak references for the keys avoids keeping the objects alive just because of their use as keys. When one has an object where other objects are registered, such as in the [[observer pattern]] (particularly in [[event handling]]), if a strong reference is kept, objects must be explicitly unregistered, otherwise a memory leak occurs (the [[lapsed listener problem]]), while a weak reference removes the need to unregister. When holding cached data that can be recreated if necessary, weak references allow the cache to be reclaimed, effectively producing discardable memory. This last case (a cache) is distinct from others, as it is preferable that the objects only be garbage collected if necessary, and there is thus a need for finer distinctions within weak references, here a stronger form of a weak reference. In many cases weak references do not need to be directly used, instead simply using a weak array or other [[Container (abstract data type)|container]] whose keys or values are weak references. ==Garbage collection== {{main|Garbage collection (computer science)}} 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>{{cite web|url=https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html|title=Practical Memory Management|website=developer.apple.com}}</ref> Indeed, even when the object graph is not a tree, a tree structure can often be imposed by the notion of object ownership, where ownership relationships are strong and form a tree, and non-ownership relationships are weak and not needed to form the tree β this approach is common in [[C++]] (pre-C++11), using raw pointers as weak references. This approach, however, has the downside of not allowing the ability to detect when a parent branch has been removed and deleted. Since the [[C++11]] standard, a solution was added by using [[Smart_pointer#shared_ptr_and_weak_ptr|shared_ptr and weak_ptr]], inherited from the [[Boost (C++ libraries)|Boost]] library. 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.{{citation needed|date=February 2023}} == Variations == Some languages have multiple levels of weak reference strength. For example, [[Java (programming language)|Java]] has, in order of decreasing strength, [[soft reference|soft]], weak, and [[phantom reference|phantom]] references, defined in the [[Java package|package]] [[Java Platform, Standard Edition#java.lang.ref|java.lang.ref]].<ref>{{cite web |last=Nicholas |first=Ethan |url=http://weblogs.java.net/blog/2006/05/04/understanding-weak-references |title=Understanding Weak References |work=java.net |date=May 4, 2006 |archive-url=https://web.archive.org/web/20110303225354/http://weblogs.java.net/blog/2006/05/04/understanding-weak-references |access-date=October 1, 2010|archive-date=2011-03-03 }}</ref> Each reference type has an associated notion of reachability. The garbage collector (GC) uses an object's type of reachability to determine when to free the object. It is safe for the GC to free an object that is softly reachable, but the GC may decide not to do so if it believes the JVM can spare the memory (e.g. the JVM has much unused heap space). The GC will free a weakly reachable object as soon as the GC notices the object. Unlike the other reference types, a phantom reference cannot be followed. On the other hand, phantom references provide a mechanism to notify the program when an object has been freed (notification is implemented using ReferenceQueues).<!-- what situations call for which type of reference --> In C#, weak references are distinguished by whether they track [[object resurrection]] or not. This distinction does not occur for strong references, as objects are not [[finalization|finalized]] if they have any strong references to them. By default, in C# weak reference do not track resurrection, meaning a weak reference is not updated if an object is resurrected; these are called '''short weak references''', and weak references that track resurrection are called '''long weak references'''.{{sfn|Goldshtein|Zurbalev|Flatow|2012|p=[https://books.google.com/books?id=D3J58cs-i44C&q=resurrection&pg=PA131 131]}} Some non-garbage-collected languages, such as [[C++]], provide weak/strong reference functionality as part of supporting garbage collection libraries. The Boost C++ library provides strong and weak references. It is a mistake to use regular C++ pointers as the ''weak'' counterparts of [[smart pointer]]s because such usage removes the ability to detect when the ''strong'' reference count has gone to 0 and the object has been deleted. Worse yet, it does not allow for detection of whether another strong reference is already tracking a given plain pointer. This introduces the possibility of having two (or more) smart pointers tracking the same plain pointer (which causes corruption as soon as one of these smart pointers' reference count reaches 0 and the object gets deleted). ==Examples== Weak references can be useful when keeping a list 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 for the duration of the program. ===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 on some platforms with dynamic heap like Android<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 is not 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>https://web.archive.org/web/20110303225354/http://weblogs.java.net/blog/2006/05/04/understanding-weak-references Java Examples</ref> <syntaxhighlight lang=java> import java.lang.ref.WeakReference; public class ReferenceTest { public static void main(String[] args) throws InterruptedException { WeakReference r = new WeakReference("I'm here"); StrongReference sr = new StrongReference("I'm here"); System.out.println("Before gc: r=" + r.get() + ", static=" + sr.get()); System.gc(); Thread.sleep(100); // Only r.get() becomes null. System.out.println("After gc: r=" + r.get() + ", static=" + sr.get()); } } </syntaxhighlight> Another use of weak references is in writing a [[cache (computing)|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. ===Smalltalk=== <syntaxhighlight lang=Smalltalk> |a s1 s2| s1 := 'hello' copy. "that's a strong reference" s2 := 'world' copy. "that's a strong reference" a := WeakArray with:s1 with:s2. a printOn: Transcript. ObjectMemory collectGarbage. a printOn: Transcript. "both elements still there" s1 := nil. "strong reference goes away" ObjectMemory collectGarbage. a printOn: Transcript. "first element gone" s2 := nil. "strong reference goes away" ObjectMemory collectGarbage. a printOn: Transcript. "second element gone" </syntaxhighlight> ===Lua=== <syntaxhighlight lang="lua"> weak_table = setmetatable({}, {__mode="v"}) weak_table.item = {} print(weak_table.item) collectgarbage() print(weak_table.item) </syntaxhighlight> ===Objective-C 2.0=== In [[Objective-C]] 2.0, not only garbage collection, but also [[Reference counting|automatic reference counting]] will be affected by weak references. All variables and properties in the following example are weak. <syntaxhighlight lang=objc> @interface WeakRef : NSObject { __weak NSString *str1; __unsafe_unretained NSString *str2; } @property (nonatomic, weak) NSString *str3; @property (nonatomic, unsafe_unretained) NSString *str4; @end </syntaxhighlight> The difference between <code>weak</code> (<code>__weak</code>) and <code>unsafe_unretained</code> (<code>__unsafe_unretained</code>) is that when the object the variable pointed to is being deallocated, whether the value of the variable is going to be changed or not. <code>weak</code> ones will be updated to [[Null pointer|<code>nil</code>]] and the <code>unsafe_unretained</code> one will be left unchanged, as a [[dangling pointer]]. The <code>weak</code> references is added to Objective-C since [[Mac OS X Lion|Mac OS X 10.7 "Lion"]] and [[iOS 5]], together with [[Xcode]] 4.1 (4.2 for iOS), and only when using ARC. Older versions of Mac OS X, iOS, and GNUstep support only <code>unsafe_unretained</code> references as weak ones. ===[[Vala (programming language)|Vala]]=== <syntaxhighlight lang="vala"> class Node { public weak Node prev; // a weak reference is used to avoid circular references between nodes of a doubly-linked list public Node next; } </syntaxhighlight> ===Python=== <syntaxhighlight lang="pycon"> >>> import weakref >>> import gc >>> class Egg: ... def spam(self): ... print("I'm alive!") ... >>> obj = Egg() >>> weak_obj = weakref.ref(obj) >>> weak_obj().spam() I'm alive! >>> obj = "Something else" >>> gc.collect() 35 >>> weak_obj().spam() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'NoneType' object has no attribute 'spam' </syntaxhighlight> == See also == * [[Soft reference]] * [[Phantom reference]] * [[Circular reference]] * [[Ephemeron]] == References == {{reflist|30em}} {{refbegin}} * {{Cite book | isbn = 978-1-4302-4458-5 | title = Pro .NET Performance: Optimize Your C# Applications | last1 = Goldshtein | first1 = Sasha | last2 = Zurbalev | first2 = Dima | last3 = Flatow | first3 = Ido | year = 2012 | publisher = Apress | url = https://academicexperts.com/programming/ }} {{refend}} == External links == === C++ === * [[C++11 | C++11 Standard Library]]: [http://en.cppreference.com/w/cpp/memory/weak_ptr <code>std::weak_ptr</code> reference] * [[Boost (C++ libraries)|Boost 1.59 (C++ library)]]: [http://www.boost.org/doc/libs/1_59_0/libs/smart_ptr/weak_ptr.htm <code>boost::weak_ptr</code> reference] === Java === * [http://www.pawlan.com/monica/articles/refobjs/ Java developer article: 'Reference Objects and Garbage Collection'] * {{cite web |last=Nicholas |first=Ethan |url=http://weblogs.java.net/blog/2006/05/04/understanding-weak-references |title=Understanding Weak References |work=java.net |date=May 4, 2006 |archive-url=https://web.archive.org/web/20100819115659/http://weblogs.java.net/blog/2006/05/04/understanding-weak-references |access-date=October 1, 2010 |archive-date=August 19, 2010 |url-status=bot: unknown }} * [http://rcache.sourceforge.net/ RCache - Java Library for weak/soft reference based cache] * [http://www.ibm.com/developerworks/java/library/j-jtp11225/ Java theory and practice: Plugging memory leaks with weak references] === PHP === * [https://www.php.net/manual/en/class.weakreference.php Weak References] === Python === * [https://docs.python.org/3/library/weakref.html weakref β Weak references β Python 3 documentation] * Fred L. Drake, Jr., ''[https://www.python.org/dev/peps/pep-0205/ PEP 205: Weak References]'', Python Enhancement Proposal, January 2001. {{Memory management navbox}} {{DEFAULTSORT:Weak Reference}} [[Category:Data types]] [[Category:Memory management]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Citation needed
(
edit
)
Template:Cite book
(
edit
)
Template:Cite web
(
edit
)
Template:Main
(
edit
)
Template:Memory management navbox
(
edit
)
Template:Refbegin
(
edit
)
Template:Refend
(
edit
)
Template:Reflist
(
edit
)
Template:Sfn
(
edit
)
Template:Short description
(
edit
)