Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 961
XCS Avatar asked Oct 10 '18 12:10

XCS


People also ask

What triggers garbage collection?

When a JVM runs out of space in the storage heap and is unable to allocate any more objects (an allocation failure), a garbage collection is triggered. The Garbage Collector cleans up objects in the storage heap that are no longer being referenced by applications and frees some of the space.

What are the limitations of garbage collection?

Drawbacks of garbage collection in Java Garbage collectors bring some runtime overhead that is out of the programmer's control. This could lead to performance problems for large applications that scale large numbers of threads or processors, or sockets that consume a large amount of memory.

What is garbage collection and when it occurs?

Garbage collection occurs when one of the following conditions is true: The system has low physical memory. This is detected by either the low memory notification from the OS or low memory as indicated by the host. The memory that's used by allocated objects on the managed heap surpasses an acceptable threshold.

What happens to the thread when garbage collection?

What happens to the thread when garbage collection kicks off? Explanation: The thread is paused when garbage collection runs which slows the application performance. 8.


Video Answer


1 Answers

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

like image 52
jmrk Avatar answered Sep 20 '22 12:09

jmrk