Can garbage collection happen while the main thread is busy?

前端 未结 2 673
闹比i
闹比i 2020-12-18 08:47

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 };
         


        
相关标签:
2条回答
  • 2020-12-18 09:24

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

    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 has the --nouse-idle-notification which in theory disables 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.

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

    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.

    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

    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 ;-)

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

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

    0 讨论(0)
  • 2020-12-18 09:31

    As a concept the garbage collector works in a separate thread since in this way it will not block the main thread (UI thread in most cases).

    As for your example, there is no problem for the garbage collection thread running in "parallel" to this loop since the value there const garbage = {key: i} will not get removed as long as it is being referenced.

    Also note that there are several generations that the garbage collector passes your values through before removing them completely.

    0 讨论(0)
提交回复
热议问题