Weak reference: Difference between revisions

Content deleted Content added
m Change "Boost framework" to "Boost library"
Citation bot (talk | contribs)
Alter: url. URLs might have been anonymized. Add: archive-date, archive-url, title. Changed bare reference to CS1/2. | Use this bot. Report bugs. | Suggested by Josve05a | Linked from User:Josve05a/cite/wayback | #UCB_webform_linked 959/1938
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]], [[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==
Line 14:
== 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=https://web.archive.org/web/20110303225354/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&pg=PA131&q=resurrection#v=onepage&q=resurrection 131]}}
Line 143:
=== Java ===
* [http://www.pawlan.com/monica/articles/refobjs/ Java developer article: 'Reference Objects and Garbage Collection']
* {{cite web |last=Nicholas |first=Ethan |url=https://web.archive.org/web/20110303225354/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 }}
* [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]