Can garbage collection happen while the main thread is busy?

前端 未结 2 685
闹比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: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.

提交回复
热议问题