主线程忙时可以进行垃圾收集吗?

2022-01-16 00:00:00 node.js garbage-collection javascript v8

假设我有一个长时间运行的循环:

Let's say I have a long running loop:

// Let's say this loop takes 10 seconds to execute
for(let i = 0; i <= 1000000; ++i) {
    const garbage = { i };
    // some other code
}

垃圾收集器可以在循环期间运行,还是只能在应用空闲时运行?

Can the garbage collector run during the loop, or it can only run when the application is idle?

我没有找到任何与此相关的文档,但是因为 Node.js 具有理论上禁用 GC 的 --nouse-idle-notification,让我认为 GC 仅在以下情况下运行发送空闲通知(当主线程不忙时).

I didn't find any documentation related to this, but because Node.js has the --nouse-idle-notification which in theory disables GC, makes me think that the GC only runs when the idle notification is sent (when the main thread is not busy).

我之所以问这个问题是因为我的循环有时会出现执行时间峰值,并且想知道 GC 是否有可能在循环期间运行,从而导致延迟峰值.

I am asking this because my loop sometimes has spikes in execution time and want to know if it's possible that the GC might run during the loop, resulting in the lag spike.

推荐答案

V8 开发者在这里.简短的回答是 GC 可以随时运行,并且会在需要时运行.

V8 developer here. The short answer is that the GC can run at any time and will run whenever it needs to.

请注意,GC 是一个相当复杂的系统:它执行多个不同的任务,并且大部分以增量步骤和/或与主线程同时执行.特别是,每次分配都会触发一些增量 GC 工作.(这意味着通过非常小心地避免所有分配,您可以构建在运行时不会导致 GC 活动的循环;但循环不会积累无法收集的垃圾——除非您的内存中有泄漏当然是代码,其中对象无意中保持可访问性.)

Note that the GC is a fairly complex system: it performs several different tasks, and does most of them in incremental steps and/or concurrently with the main thread. In particular, every allocation can trigger a bit of incremental GC work. (Which implies that by very carefully avoiding all allocations, you can construct loops that won't cause GC activity while they run; but it's never the case that loops accumulate garbage that can't get collected -- unless you have a leak in your code of course, where objects are unintentionally being kept reachable.)

垃圾收集器可以在循环期间运行,还是只能在应用空闲时运行?

Can the garbage collector run during the loop, or it can only run when the application is idle?

它绝对可以并且将在循环期间运行.

It absolutely can and will run during the loop.

Node.js 有 --nouse-idle-notification 理论上禁用 GC

Node.js has the --nouse-idle-notification which in theory disables GC

不,它没有.没有办法禁用 GC.该标志禁用了触发 GC 活动的一种特定机制,但这仅意味着 GC 将由其他机制触发.

No, it does not. There is no way to disable GC. That flag disables one particular mechanism for triggering GC activity, but that only means that GC will be triggered by other mechanisms.

GC 只在发送空闲通知时运行(当主线程不忙时)

the GC only runs when the idle notification is sent (when the main thread is not busy)

不,我们的想法是在空闲时间运行一些额外个 GC 周期,以便在应用程序不忙时节省一些内存.

No, the idea is to run some extra GC cycles when there is idle time, to save some memory when the application is not busy.

我的循环有时会出现执行时间峰值,想知道 GC 是否有可能在循环期间运行,从而导致延迟峰值

my loop sometimes has spikes in execution time and want to know if it's possible that the GC might run during the loop, resulting in the lag spike

可能是这样.它也可能与函数的优化或去优化有关.或者它可能是其他原因——例如,操作系统中断了您的进程或将其分配给另一个 CPU 内核,或数百个其他原因.计算机是复杂的机器;-)

That could be. It could possibly also have to do with optimization or deoptimization of the function. Or it could be something else -- the operating system interrupting your process or assigning it to another CPU core, for example, or hundreds of other reasons. Computers are complex machines ;-)

如果您将变量设置为 null -- 垃圾收集会立即完成

if you set a variable to null -- garbage collection is done immediately

不,不是.垃圾收集永远不会立即完成(至少在 V8 中不会).

No, it is not. Garbage collection is never done immediately (at least not in V8).

相关文章