JavaScript承诺在未被拒绝或解决时是否会造成内存泄漏? [英] Does JavaScript promise create memory leaks when not rejected or resolved?

查看:53
本文介绍了JavaScript承诺在未被拒绝或解决时是否会造成内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到需要在并行中执行异步函数的情况,并以最佳结果继续执行程序。因此我写了这样的东西:

I'm in a situation where I need execute async functions in "parallel", and continue program execution with the best result. Thus I wrote something like this :

var p = [];

for (var i = 0; i < 10; ++i) (function (index) {
  p.push(new Promise(function (resolve, reject) {
    setTimeout(function () {
      var success = Math.random() > 0.7;

      console.log("Resolving", index, "as", success ? "success" : "failure");

      success && resolve(index);
    }, Math.random() * 5000 + 200);
  }));
})(i);

Promise.race(p).then(function (res) {
  console.log("FOUND", res);
}).catch(function (err) {
  console.log("ERROR", err);
});

现在,我想知道在使用promises时这是否是一个好习惯?是不是更频繁地解决或拒绝它们然后任何事情造成内存泄漏?他们每次最终都是GC吗?

Now, I'm wondering if this is good practice when working with promises? Is not resolving or rejecting them more often then anything create memory leaks? Are they all eventually GC'ed every time?

推荐答案

这会泄漏的唯一原因是因为 p 是全球性的。最后设置 p = null; ,或者避免使用全局变量:

The only reason this will leak is because p is a global. Set p = null; at the end, or avoid using a global variable:

var console = { log: function(msg) { div.innerHTML += msg + "<br>"; }};

Promise.race(new Array(10).fill(0).map(function(entry, index) {
  return (function(index) {
    return new Promise(function(resolve) {
      setTimeout(function() {
        var success = Math.random() > 0.7;
        console.log((success? "R":"Not r") + "esolving "+ index +".");
        success && resolve(index);
      }, Math.random() * 5000 + 200);
    });
  })(index);
})).then(function (res) {
    console.log("FOUND: " + res);
}).catch(function (err) {
    console.log("ERROR: " + err);
});

<div id="div"></div>

Promise.race 只要在<$中输入一个 p c $ c> p 成功或失败,以较早者为准, setTimeout 将在5.2秒后释放所有内容。然后JavaScript会愉快地收集承诺,无论它们是已被解决,被拒绝还是两者都没有。没有坏处。

Promise.race lets go of p as soon as one entry in p succeeds or something fails, whichever is sooner, and setTimeout will have let go of everything after 5.2 seconds. JavaScript will then happily garbage collect the promises whether they've been resolved, rejected or neither. No harm.

你唯一想避免的是垃圾收集被拒绝的承诺,因为它可能会触发浏览器警告,因为它表示网络编程错误。

The only thing you want to avoid is garbage collecting rejected promises, as that is likely to trigger a browser warning, because it is indicative of a web programming bug.

当然,有3%的可能性没有解决,在这种情况下,这将泄漏(直到你关闭标签)。

Of course, there's a 3% chance none of them resolve, in which case this will leak (until you close the tab).

我认为这取决于功能的作用。如果它们是轻量级的,那么我没有看到问题。如果他们正在进行繁重的计算或产生副作用,那么他们希望有一些API来取消操作,以帮助节省资源。

I think it depends on what the functions are doing. If they are lightweight, then I don't see a problem. If they are doing heavy computations or have side-effects, then they hopefully come with some API to cancel the operations, to help save on resources.

这篇关于JavaScript承诺在未被拒绝或解决时是否会造成内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆