Garbage collection in generators

Benjamin Gruenbaum benjamingr at gmail.com
Wed Feb 17 08:08:27 UTC 2016


In the following example:

```js

function* foo() {
    try {
       yield 1;
    } finally {
        cleanup();
    }
}
(function() {
  var f = foo();
  f.next();
  // never reference f again
})()

```

 - Is the iterator created by the function `foo` ever eligible for garbage
collection?
 - If it is - does it run the `finally` blocks?


Related resources:

 - Python changed the behavior to "run `return` on gc" in 2.5
https://docs.python.org/2.5/whatsnew/pep-342.html
 - C# doesn't run finalizers, but iterators are disposable and get aborted
automatically by foreach (for... of) - on break. this is similar to what we
do:
http://blogs.msdn.com/b/dancre/archive/2008/03/14/yield-and-usings-your-dispose-may-not-be-called.aspx
 - PHP is debating this issue now, I was contacted by PHP internals people
about it which is how I came into the problem in the first place:
https://bugs.php.net/bug.php?id=71604
 - Related issue I opened on async/await :
https://github.com/tc39/ecmascript-asyncawait/issues/89
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160217/ebcb478e/attachment-0001.html>


More information about the es-discuss mailing list