Interesting impact on other specifications #31
Comments
Specifying the semantics of gc is hard. At http://web.archive.org/web/20160318124045/http://wiki.ecmascript.org/doku.php?id=strawman:gc_semantics I suggest an upper bound on what may be collected (i.e., a lower bound on what must be retained):
See the three papers linked at the bottom of that page. They are good but I no longer remember them in any detail. |
Hmm. I doubt this is really a problem. The web platform doesn't specify a GC algorithm that has a mark phase and unconditionally visits every internal field. x can be collected, even though y is specified to have a field that points to it, because x is unreachable in the plain English sense—it can't be reached. The field is dead. |
Interestingly, this specification itself faces these liveness issues, in #66. Overall, though, I don't think we should take any action here on the specification side, since spec datastructure reachability already isn't the definition of reachability by garbage collectors. For example, the following code sample shouldn't hold up (function() {
const bigData = { /* lots of stuff */ };
const group = new FinalizationGroup(iterator => {
for (const item of iterator) console.log(`collected ${item}`);
});
group.register(bigData, "data");
setTimeout(() => console.log("timeout"), 1000);
})(); This should be allowed to print out:
However, in terms of spec data structures, the environment record which points to There's still a practical issue about potential compatibility issues where one engine has a more precise definition of liveness than another. But JavaScript contexts are already a very present case of this, where developers already report out-of-memory issues that exist on some browsers and not others, which can be traced back to diverging issues of liveness. The details of streams' liveness will be much smaller in magnitude than closures, but at the same time, there's no clear path to compatibility in the closure case. |
Anyway, maybe we should mention this to the TAG and get their take as well. We should document clear guidance for specification authors about what all this means. |
This attempts to address various issues with how reachability is defined: - If one WeakRef object points to an object, which points to another one, the whole subgraph may be collected, as raised in https://gist.github.com/jimblandy/0014dc11233d2d40df922af850b0489a - An initial provisional definition of collectability is given: > No possible future execution of the program would observably > reference the object, except through a chain of references which > includes a [[Target]] field or [[Target]] internal slot. This is intended to resolve tc39#31 (by concluding that the impact on host specifications is minimal, and spec graph reachability does not imply liveness) and provide a starting point for tc39#105 - The wording around 'field or internal slot' is clarified, resolving tc39#115
This attempts to address various issues with how reachability is defined: - If one WeakRef object points to an object, which points to another one, the whole subgraph may be collected, as raised in https://gist.github.com/jimblandy/0014dc11233d2d40df922af850b0489a - An initial provisional definition of collectability is given: > No possible future execution of the program would observably > reference the object, except through a chain of references which > includes a [[Target]] field or [[Target]] internal slot. This is intended to resolve #31 (by concluding that the impact on host specifications is minimal, and spec graph reachability does not imply liveness) and provide a starting point for #105 - The wording around 'field or internal slot' is clarified, resolving #115
Reopening this issue to discuss possibilities for spec notation for weak references; see #168 (comment) . |
whatwg/streams#932 by @ricea brings up an interesting situation. I'm curious what the champions have to say about this, although I don't anticipate any changes to the weak refs proposal because of it.
The summary is:
y
holds on to a user-provided objectx
indefinitelyy.close()
,y
will never usex
.x
to be GCed beforey
.With weak refs, this of course becomes observable. Thus once weak refs are introduced, the spec as-written mandates that
x
never be GCed beforey
.In reaction to this, the spec author should probably change
y.close()
to null out the internal reference tox
explicitly. Then browsers will again be allowed to collectx
beforey
and free up some memory, while still conforming to the spec.Generalizing, the tricky parts of the situation are:
The text was updated successfully, but these errors were encountered: