Weak reference: Difference between revisions

Content deleted Content added
Citation bot (talk | contribs)
Misc citation tidying. | Use this bot. Report bugs. | Suggested by AManWithNoPlan | #UCB_CommandLine
Bender the Bot (talk | contribs)
m Java: HTTP to HTTPS for SourceForge
 
(11 intermediate revisions by 9 users not shown)
Line 1:
{{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> [[Racket (programming language)|Racket]], 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==
Line 19:
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 doesn'tdoes 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.
 
===JavaC#===
[[C Sharp (programming language)|C#]] have the {{Mono|WeakReference}} class.<ref>{{cite web |title=Weak References - .NET |url=https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/weak-references |website=learn.microsoft.com |access-date=9 July 2025 |language=en-us}}</ref><ref>{{cite web |title=WeakReference Class (System) |url=https://learn.microsoft.com/en-us/dotnet/api/system.weakreference?view=net-9.0 |website=learn.microsoft.com |access-date=9 July 2025 |language=en-us}}</ref>
<syntaxhighlight lang="valacsharp">
new Dictionary<int, WeakReference>();
</syntaxhighlight lang=Smalltalk>
 
===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"soft reference”reference" (intended to be used for maintaining GC-managed in-memory caches, but which doesn’tdoesn'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"weak reference”reference". It also added a related experimental mechanism dubbed “phantom"phantom references”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'tis 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;
 
Line 49 ⟶ 54:
 
===Smalltalk===
<syntaxhighlight lang="smalltalk">
 
<syntaxhighlight lang=Smalltalk>
|a s1 s2|
 
Line 70 ⟶ 74:
 
===Lua===
 
<syntaxhighlight lang="lua">
weak_table = setmetatable({}, {__mode="v"})
Line 81 ⟶ 84:
===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
{
Line 95 ⟶ 98:
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&nbsp;OS&nbsp;X&nbsp;10.7&nbsp;"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.
 
===PHP===
===[[Vala (programming language)|Vala]]===
[[PHP]] have the {{Mono|WeakReference}} class.<ref>{{cite web |title=PHP: WeakReference - Manual |url=https://www.php.net/manual/en/class.weakreference.php |website=www.php.net |access-date=9 July 2025 |language=en}}</ref>
<syntaxhighlight lang="vala">
<syntaxhighlight lang="php">
class Node {
$obj = new stdClass();
public weak Node prev; // a weak reference is used to avoid circular references between nodes of a doubly-linked list
$weakref = WeakReference::create($obj);
public Node next;
 
var_dump($weakref->get());
unset($obj);
var_dump($weakref->get());
</syntaxhighlight>
 
===Python===
[[Python (programming language)|Python]] have the {{Mono|weakref}} module.<ref>{{cite web |title=weakref — Weak references |url=https://docs.python.org/3/library/weakref.html |website=Python documentation |access-date=9 July 2025 |language=en}}</ref>
<syntaxhighlight lang="pycon">
>>> import weakref
Line 122 ⟶ 129:
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'spam'
</syntaxhighlight>
 
===Vala===
[[Vala (programming language)|Vala]] use the {{Mono|weak}} keyword.<ref>{{cite web |title=4.9. Weak References - Vala Documentation |url=https://docs.vala.dev/tutorials/programming-language/main/04-00-advanced-features/04-09-weak-references.html |website=docs.vala.dev |access-date=9 July 2025}}</ref>
<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>
 
Line 133 ⟶ 149:
{{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 = httphttps://www.apressacademicexperts.com/9781430244585programming/ }}
{{refend}}
 
Line 144 ⟶ 160:
=== 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://academicexpertsweb.comarchive.org/programmingweb/20100819115659/http://weblogs.java.net/blog/2006/05/04/understanding-weak-references |access-date=October 1, 2010 |archive-date=2011August 19, 2010 |url-03-03status=bot: unknown }}
* [httphttps://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]