Jump to content

Talk:Weak reference

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by SineBot (talk | contribs) at 01:42, 24 February 2015 (Signing comment by 81.191.74.4 - "Implementation: new section"). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
WikiProject iconComputing C‑class Low‑importance
WikiProject iconThis article is within the scope of WikiProject Computing, a collaborative effort to improve the coverage of computers, computing, and information technology on Wikipedia. If you would like to participate, please visit the project page, where you can join the discussion and see a list of open tasks.
CThis article has been rated as C-class on Wikipedia's content assessment scale.
LowThis article has been rated as Low-importance on the project's importance scale.

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)[reply]

Done ... with links to their own articles. Stumps 00:05, 7 February 2007 (UTC)[reply]
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)[reply]

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)[reply]


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)[reply]

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)[reply]

Java example

Currently, this article uses this Java example:

import java.lang.ref.WeakReference;

public class ReferenceTest {
	public static void main(String[] args) throws InterruptedException {

            WeakReference r = new WeakReference(new String("I'm here"));
            WeakReference sr = new WeakReference("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());

	}
}

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

  • 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?

--Abdull (talk) 23:00, 10 December 2012 (UTC)[reply]

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)[reply]

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)[reply]