Talk:Weak reference

From Wikipedia, the free encyclopedia
Jump to: navigation, search
WikiProject Computing (Rated C-class, Low-importance)
WikiProject icon This 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.
C-Class article C  This article has been rated as C-Class on the project's quality scale.
 Low  This 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)

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)

Java example[edit]

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)