Garbage collection in generators

Boris Zbarsky bzbarsky at mit.edu
Wed Feb 17 15:54:34 UTC 2016


On 2/17/16 3:59 AM, Benjamin Gruenbaum wrote:
> Garbage collection can and does in fact manage resources in JavaScript
> host environments right now. For example, an XMLHttpRequest /may /abort
> the underlying HTTP request if the XMLHttpObject is not referenced
> anywhere and gets garbage collected.

If an implementation does that, it's clearly buggy.  Consider:

   function foo() {
     var xhr = new XMLHttpRequest();
     xhr.addEventListener("load", function() {
       alert(this.responseText);
     }
     xhr.open(stuff);
     xhr.send();
   }
   foo();

The XMLHttpRequest object is not "referenced anywhere" in JS terms 
between foo() returning and the load event being fired.  But the load 
event really does need to be fired.  Can you point me to the spec text 
that makes you think that in this situation not firing the load event 
would be an OK thing to do?

In practice what this means is that the UA needs to keep the object 
alive and prevent it being garbage collected until the end of the HTTP 
response is received.

-Boris

P.S. 
https://groups.google.com/d/msg/mozilla.dev.tech.js-engine.internals/V__5zqll3zc/hLJiNqd8Xq8J 
has some comments on this exact issue of resource management via GC.


More information about the es-discuss mailing list