Talk:Weak reference

It would be nice to include a description of Java's phantom and soft references here. -- Doug Bell ( talk/contrib ) 11:29, 13 January 2006 (UTC)
 * Done ... with links to their own articles. Stumps 00:05, 7 February 2007 (UTC)
 * I don't think that the part that says "Another use of weak references is in writing a cache." should be there: soft references should be used for that purpose (because they are defined to take memory pressure into account, which is normally what you want for a cache). Nicolas Barbier (talk) 17:10, 14 December 2010 (UTC)

Also, isn't using normal pointers as weak references in C++ with garbage collection libraries different semantically than using a weak reference in Java? I don't believe there is any way to tell using a normal C++ pointer if the referent object has been collected. Without this capability, a C++ "weak reference" can't be used for resource management. I don't know if this is the same for Python weak references, but I think all of this information would make an interesting expansion to the artice. -- Doug Bell ( talk/contrib ) 11:29, 13 January 2006 (UTC)

How about adding info about how this is accomplished in C++ (i.e. boost::weak_ptr and boost::shared_ptr)? Just a suggestion. :) --Antred11 (talk) 16:38, 7 March 2011 (UTC)

Weak references can also mean something completely unrelated, mainly how symbols are resolved in a runtime. Weak references are those symbols in an executable which may be overridden at runtime, usually by loading an optional dynamic library. See http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html Corydon76 (talk) 09:00, 10 December 2011 (UTC)

I suggest to change the description about boost::weak_ptr to std::weak_ptr, since C++11 is already widely used and accepted and of course it is better to mention solutions which use native structures, rather than third-party. Otstoy (talk) 17:57, 18 November 2015 (UTC)

Java example
Currently, this article uses this Java example: In my opinion, this example doesn't clarify the topic at hand, but instead raises new questions.

--Abdull (talk) 23:00, 10 December 2012 (UTC)
 * Why is it that r's String gets garbage-collected, but sr's String does not? Is this because of string interning? (new String("I'm here") versus "I'm here")
 * What happens when garbage collection spuriously kicks in concurrently right after the first line? Would this make the first println statement already return r=null?
 * If weak references can be garbage collected, is there the danger of having a weak reference be garbage collected right after the moment it was created - already rendering the object useless before any sensible computation with this object took place?
 * Is the WeakReference class the only way of creating weak references in a Java JVM environment?

2A02:C7D:A1DA:D200:AD3D:BA14:ECF9:245B (talk) 11:20, 24 November 2019 (UTC)
 * I would reckon that the sr string is still "in scope" whereas the r string is not. The sr string must have a strong reference for the duration of the function, but the r string does not. If you were to write
 * String s = new String("I'm here"); // strong reference
 * WeakReference r = new WeakReference(s); // not Gc'd until closing brace

removing weird Java text
The article contained this text:


 * Java was the first main language to introduce strong reference as the default object reference. Previously (ANSI) C only had weak references. However it was noted by David Hostettler Wain and Scott Alexander Nesmith that event trees were not evaporating properly. Thus strong/weak references of counted and uncounted references was derived (~1998).

I can’t make head or tail of this. I don’t know what a “main language” might be, Java from 1996 has the same kind of strong references Lisp had in 1959, C doesn’t have references at all, I don’t know who these Wain and Nesmith guys are, I don’t know what event trees are or why they might evaporate or what that has to do with weak references. I suspect this text may have been translated from a Chinese Wikipedia article but not so well that I can figure out what it’s saying. I tried to find the Wain & Nesmith 1998 text that is alluded to, but without success.

Feel free to re-add what this text was trying to say. I’ve replaced it with an introduction to the history of weak references in Java. Kragen Javier Sitaker (talk) 21:26, 17 February 2015 (UTC)


 * Wain is user:Davew123, who has spent years inserting himself into various articles. 80.189.137.19 (talk) 13:56, 19 April 2015 (UTC)

Implementation
A description of how weak references are implemented would be useful. — Preceding unsigned comment added by 81.191.74.4 (talk) 01:41, 24 February 2015 (UTC)

JavaScript WeakMap
JavaScript supports associative arrays with weak references as keys, although enumerating the keys is not possible in this case. --Zzo38 (talk) 19:42, 31 August 2016 (UTC)