Weak reference: Difference between revisions

Content deleted Content added
m Reverted edits by 87.61.119.229 (talk) (HG) (3.4.10)
Monkbot (talk | contribs)
m Task 18 (cosmetic): eval 7 templates: del empty params (1×); hyphenate params (2×);
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 |accessdateaccess-date=October 1, 2010}}</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 132:
{{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 = http://www.apress.com/9781430244585 | pages = }}
{{refend}}
 
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 |accessdateaccess-date=October 1, 2010}}
* [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]