这个功能可以被垃圾收集吗?

时间:2022-06-01 21:28:49

Consider this piece of cake... ehm, code:

考虑一下这块蛋糕......呃,代码:

'use strict'

function doWork () {
  return new Promise(function (resolve, reject) {
    // work work work...
    // Done! But... where's the resolve() ???
  })
}

doWork().then(function doMoreWork () {
  // Some more work to do...
})

Once the function in the Promise's constructor finishes...

一旦Promise的构造函数中的函数完成......

  1. Is the Promise object garbage-collectible?
  2. Promise对象是垃圾收集的吗?
  3. Is doMoreWork() garbage-collectible?
  4. doMoreWork()是垃圾收集的吗?

My guess is that doMoreWork() cannot be GC-ed directly because the Promise keeps a reference to it, but once the promise's body finishes and returns the execution context to the upper (?) scope, the stack unwinds (because there is no more statements here to be executed) and the Promise becomes unreachable, thus being garbage-collectible.

我的猜测是doMoreWork()不能直接进行GC编辑,因为Promise会保留对它的引用,但是一旦promise的主体完成并将执行上下文返回到上部(?)范围,堆栈就会展开(因为没有更多)这里要执行的语句)和Promise变得无法访问,因此是垃圾收集的。

Can you confirm that my understanding of this topic is correct?

你能否证实我对这个主题的理解是正确的?

How could I empirically observe this behaviour? In other words, how can I monitor what objects are being GC-ed and when? I develop purely in Node.js, if that makes any difference.

我怎么能凭经验观察这种行为?换句话说,如何监控哪些对象正在进行GC编辑以及何时进行?我纯粹在Node.js中开发,如果这有任何区别。

3 个解决方案

#1


1  

There is nothing keeping reference to the promise so it will be garbage collected. The promise is the only thing keeping reference to the function doMoreWork so it will be garbage collected too.

什么都没有保留对承诺的引用,因此它将被垃圾收集。承诺是唯一保持对函数doMoreWork的引用,因此它也将被垃圾收集。

How could I empirically observe this behaviour? In other words, how can I monitor what objects are being GC-ed and when? I develop purely in Node.js, if that makes any difference.

我怎么能凭经验观察这种行为?换句话说,如何监控哪些对象正在进行GC编辑以及何时进行?我纯粹在Node.js中开发,如果这有任何区别。

The GC in V8 never necessarily collects an object. For instance if this is your whole program, it would be a waste of time to run any GC in the first place.

V8中的GC不一定会收集对象。例如,如果这是您的整个程序,那么首先运行任何GC都是浪费时间。

#2


0  

  1. The Promise object is collectable if it has no references pointing to it. If it is used doWork().then(...) a temporary reference is created. So until .then does not block anymore there is a reference to the object so it cannot be collected
  2. 如果Promise对象没有指向它的引用,则它是可收集的。如果使用doWork()。则(...)创建临时引用。因此,直到.then不再阻止有对该对象的引用,因此无法收集它
  3. You're right, doMoreWork is also not collectible because the Promiseobject has a reference to it
  4. 你是对的,doMoreWork也是不可收集的,因为Promiseobject有一个对它的引用

The statement doWork().then(...) can be replaced by

语句doWork()。then(...)可以替换为

new Promise(function (resolve, reject) {
  // work work work...
}).then(function doMoreWork () {
          // Some more work to do...
        })

So you can imagine that you are using the Promiseobject directly, so the "Upper"-Scope is where the object is used.

因此,您可以想象您正在使用Promiseobject,因此“Upper”-Scope是使用对象的位置。

Objects are generally collected when there are no more references to it. Even if the code is in a Promise it is just an object and the call to then is chained, so the object is being used

通常在没有对象的引用时收集对象。即使代码在Promise中,它只是一个对象,并且对then的调用是链接的,因此正在使用该对象

#3


0  

To see if an object is garbage-collectible you can create a test and look for memory leak (through task manager). If your code is written properly, everything gets collected.

要查看对象是否是垃圾可收集的,您可以创建测试并查找内存泄漏(通过任务管理器)。如果您的代码写得正确,一切都会被收集。

#1


1  

There is nothing keeping reference to the promise so it will be garbage collected. The promise is the only thing keeping reference to the function doMoreWork so it will be garbage collected too.

什么都没有保留对承诺的引用,因此它将被垃圾收集。承诺是唯一保持对函数doMoreWork的引用,因此它也将被垃圾收集。

How could I empirically observe this behaviour? In other words, how can I monitor what objects are being GC-ed and when? I develop purely in Node.js, if that makes any difference.

我怎么能凭经验观察这种行为?换句话说,如何监控哪些对象正在进行GC编辑以及何时进行?我纯粹在Node.js中开发,如果这有任何区别。

The GC in V8 never necessarily collects an object. For instance if this is your whole program, it would be a waste of time to run any GC in the first place.

V8中的GC不一定会收集对象。例如,如果这是您的整个程序,那么首先运行任何GC都是浪费时间。

#2


0  

  1. The Promise object is collectable if it has no references pointing to it. If it is used doWork().then(...) a temporary reference is created. So until .then does not block anymore there is a reference to the object so it cannot be collected
  2. 如果Promise对象没有指向它的引用,则它是可收集的。如果使用doWork()。则(...)创建临时引用。因此,直到.then不再阻止有对该对象的引用,因此无法收集它
  3. You're right, doMoreWork is also not collectible because the Promiseobject has a reference to it
  4. 你是对的,doMoreWork也是不可收集的,因为Promiseobject有一个对它的引用

The statement doWork().then(...) can be replaced by

语句doWork()。then(...)可以替换为

new Promise(function (resolve, reject) {
  // work work work...
}).then(function doMoreWork () {
          // Some more work to do...
        })

So you can imagine that you are using the Promiseobject directly, so the "Upper"-Scope is where the object is used.

因此,您可以想象您正在使用Promiseobject,因此“Upper”-Scope是使用对象的位置。

Objects are generally collected when there are no more references to it. Even if the code is in a Promise it is just an object and the call to then is chained, so the object is being used

通常在没有对象的引用时收集对象。即使代码在Promise中,它只是一个对象,并且对then的调用是链接的,因此正在使用该对象

#3


0  

To see if an object is garbage-collectible you can create a test and look for memory leak (through task manager). If your code is written properly, everything gets collected.

要查看对象是否是垃圾可收集的,您可以创建测试并查找内存泄漏(通过任务管理器)。如果您的代码写得正确,一切都会被收集。