主线程繁忙时可以进行垃圾回收吗? [英] Can garbage collection happen while the main thread is busy?

查看:136
本文介绍了主线程繁忙时可以进行垃圾回收吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的循环很长:

// 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具有--nouse-idle-notification,理论上它禁用了GC,因此我认为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)

否,我们的想法是在空闲时间运行一些 extra 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).

这篇关于主线程繁忙时可以进行垃圾回收吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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