简体   繁体   English

JavaScript 垃圾收集器何时以及如何工作

[英]When and How JavaScript garbage collector works

I did read few articles like this on MDN and this one I got the idea of how GC happens in JavaScript我确实在 MDN 上读过几篇这样的文章, 这篇文章让我了解了 GC 在 JavaScript 中是如何发生的

I still don't understand things like我仍然不明白之类的东西

a) When does Garbage collector kicks in ( it gets called after some interval or some conditions have to met)? a) 垃圾收集器何时启动(它在某个时间间隔或必须满足某些条件后被调用)?

b) Who is responsible for Garbage collection ( it's part of JavaScript engine or browser/Node )? b) 谁负责垃圾收集(它是 JavaScript 引擎或浏览器/节点的一部分)?

c) runs on main thread or separate thread? c) 在主线程或单独线程上运行?

d) which one of the following have higher peak memory usage? d) 以下哪一项具有更高的峰值 memory 使用?

// first-case
// variables will be unreachable after each cycle

(function() {
  for (let i = 0; i < 10000; i++) {
    let name = 'this is name' + i;
    let index = i;
  }
})()
// second-case
// creating variable once

(function() {
  let i, name, index;

  for (i = 0; i < 10000; i++) {
    name = 'this is name' + i;
    index = i;
  }
})()

V8 developer here. V8 开发人员在这里。 The short answer is: it's complicated .简短的回答是:它很复杂 In particular, different JavaScript engines, and different versions of the same engine, will do things differently.特别是,不同的 JavaScript 引擎,以及同一引擎的不同版本,会做不同的事情。

To address your specific questions:要解决您的具体问题:

a) When does Garbage collector kicks in ( it gets called after some interval or some conditions have to met)? a) 垃圾收集器何时启动(它在某个时间间隔或必须满足某些条件后被调用)?

Depends.要看。 Probably both.大概两者兼而有之。 Modern garbage collectors often are generational: they have a relatively small "young generation", which gets collected whenever it is full.现代垃圾收集器通常是分代的:它们有一个相对较小的“年轻一代”,只要它满了就会被收集。 Additionally they have a much larger "old generation", where they typically do their work in many small steps, so as to never interrupt execution for too long.此外,他们有一个更大的“老一代”,他们通常在许多小步骤中完成他们的工作,以便永远不会中断执行太长时间。 One common way to trigger such a small step is when N bytes (or objects) have been allocated since the last step.触发这样一个小步骤的一种常见方法是自上一步以来已经分配了 N 个字节(或对象)。 Another way, especially in modern tabbed browsers, is to trigger GC activity when a tab is inactive or in the background.另一种方法,尤其是在现代选项卡式浏览器中,是在选项卡处于非活动状态或在后台时触发 GC 活动。 There may well be additional triggers beyond these two.除了这两个之外,很可能还有其他触发因素。

b) Who is responsible for Garbage collection ( it's part of JavaScript engine or browser/Node )? b) 谁负责垃圾收集(它是 JavaScript 引擎或浏览器/节点的一部分)?

The garbage collector is part of the JavaScript engine.垃圾收集器是 JavaScript 引擎的一部分。 That said, it must have certain interactions with the respective embedder to deal with embedder-managed objects (eg DOM nodes) whose lifetime is tied to JavaScript objects in one way or another.也就是说,它必须与相应的嵌入器进行某些交互,以处理其生命周期以一种或另一种方式与 JavaScript 对象相关联的嵌入器管理的对象(例如 DOM 节点)。

c) runs on main thread or separate thread? c) 在主线程或单独线程上运行?

Depends.要看。 In a modern implementation, typically both: some work happens in the background (in one or more threads), some steps are more efficient to do on the main thread.在现代实现中,通常两者都有:一些工作发生在后台(在一个或多个线程中),一些步骤在主线程上执行效率更高。

d) which one of the following have higher peak memory usage? d) 以下哪一项具有更高的峰值 memory 使用?

These two snippets will (probably) have the same peak memory usage: neither of them ever lets objects allocated by more than one iteration be reachable at the same time.这两个片段将(可能)具有相同的峰值 memory 用法:它们都不允许同时访问由多个迭代分配的对象。


Edit: if you want to read more about recent GC-related work that V8 has been doing, you can find a series of blog posts here: https://v8.dev/blog/tags/memory编辑:如果您想了解更多有关 V8 近期 GC 相关工作的信息,您可以在此处找到一系列博客文章: https://v8.dev/blog/tags/memory

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM