title: Can garbage collection happen while the main thread is busy?
body: <p>Let's say I have a long running loop:</p>
  
  <pre><code>// Let's say this loop takes 10 seconds to execute
  for(let i = 0; i &lt;= 1000000; ++i) {
      const garbage = { i };
      // some other code
  }
  </code></pre>
  
  <p>Can the garbage collector run during the loop, or it can only run when the application is idle?</p>
  
  <p>I didn't find any documentation related to this, but because Node.js has the <code>--nouse-idle-notification</code> 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).</p>
  
  <p>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.</p>