What is the status of Weak References?

David Bruant bruant.d at gmail.com
Sun Feb 3 06:41:04 PST 2013


Le 03/02/2013 12:08, Kevin Gadd a écrit :
> On Sun, Feb 3, 2013 at 2:58 AM, David Bruant <bruant.d at gmail.com> wrote:
>> Let's see how the example would be with weakrefs:
>>
>>      function Storage(){
>>          var storage = []
>>          return {
>>              push(e){storage.push(makeWeakRef(e))},
>>              last(){
>>                  var last = storage[storage.length-1]
>>                  return last.get(); // oops!
>>              };
>>          }
>>      }
>>
>>      var s = new Storage();
>>      s.push({});
>>      s.push({});
>>      s.last();
> What problem is this example supposed to be solving?
None, but that's beyond the point. My point was to explain that in some 
cases a human being can see that some objects aren't going to be needed 
any longer while the GC algorithm cannot. The reason is that nowadays, 
state-of-the-art GCs are oblivious to the code semantics.

> The problem here is not weakrefs, the problem is that the problem is poorly specified
I didn't say the problem came from weakrefs, but that weakrefs don't 
help for this particular problem.

> When discussing issues as complex as garbage collection, the examples
> need to be at least vaguely real-world. Your example does not
> demonstrate a limitation of GCs because there's nothing for the GC to
> actually do.
It's really hard to find real code, because it often means showing 
several pages of code and I don't want to force everyone reading to 
understand several pages of code just to make a point.
Although my example is dummy, it supports the point that a GC is limited 
because it's oblivious to code semantics and limits the information it 
uses to "object", "reference" (and "root" for some objects in a 
mark-and-sweep). WeakRefs would just add a word in the GC vocabulary 
("weak reference"), but wouldn't change the obliviousness.

On the obliviousness, here is another example. Let's say jQuery is 
imported with requireJS (otherwise, it's global and you can't do 
anything really). If you use only one jQuery function, the GC could 
probably figure out that only one function is used and release the rest. 
It doesn't, because you need a reference to the main jQuery object and 
the rest is attached and the GC has no understanding that you're using 
only one function of this library.
Weakrefs cannot help with this problem. Hopefully, that's real world 
enough even without the code ;-)
There will always be problems that weak references won't be able to 
solve. That's all I'm saying.

[answering to another point from yesterday]
> I agree that there are scenarios where .dispose() or a similar
> protocol may be come necessary; in those scenarios it would be great
> if JS had some mechanism that could be used to simplify the proper use
> of the protocol. Given that the need for Weak References in many
> scenarios would be reduced because a partially (or wholly) automatic
> implementation of that protocol can make it much easier to use the
> protocol correctly.
The idea of a protocol is very interesting. I would love to see 
different *deterministic* protocols explored before bringing weakrefs if 
possible.

David


More information about the es-discuss mailing list