Search code examples
javascriptnode.jsgarbage-collectionv8

Can garbage collection happen while the main thread is busy?


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?

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

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.


Solution

  • 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).