本文介绍了JavaScript 垃圾收集器何时以及如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实在 MDN 上阅读了一些类似此内容的文章这个 我知道如何GC 发生在 JavaScript 中

I did read few articles like this on MDN and this one I got the idea of how GC happens in JavaScript

我还是不明白

a) 垃圾收集器什么时候开始(它在某个时间间隔或必须满足某些条件后被调用)?

a) When does Garbage collector kicks in ( it gets called after some interval or some conditions have to met) ?

b) 谁负责垃圾收集(它是 JavaScript 引擎或浏览器/节点的一部分)?

b) Who is responsible for Garbage collection ( it's part of JavaScript engine or browser/Node ) ?

c) 在主线程或单独线程上运行?

c) runs on main thread or separate thread ?

d) 以下哪一项具有更高的峰值内存使用率?

d) which one of the following have higher peak memory usage ?

// 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 开发人员在这里.简短的回答是:这很复杂.特别是,不同的 JavaScript 引擎,以及同一引擎的不同版本,会做不同的事情.

V8 developer here. The short answer is: it's complicated. In particular, different JavaScript engines, and different versions of the same engine, will do things differently.

解决您的具体问题:

a) 垃圾收集器什么时候开始(它在某个时间间隔或必须满足某些条件后被调用)?

视情况而定.可能两者都有.现代垃圾收集器通常是分代的:他们有一个相对较小的年轻代",只要它满了就会被收集.此外,他们有一个更大的老年代",他们通常以许多小步骤完成工作,以免中断执行时间太长.触发这样一个小步骤的一种常见方法是自上一步以来已经分配了 N 个字节(或对象).另一种方式,尤其是在现代标签浏览器中,是在标签处于非活动状态或在后台时触发 GC 活动.除了这两个之外,很可能还有其他触发因素.

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. Another way, especially in modern tabbed browsers, is to trigger GC activity when a tab is inactive or in the background. There may well be additional triggers beyond these two.

b) 谁负责垃圾收集(它是 JavaScript 引擎或浏览器/节点的一部分)?

垃圾收集器是 JavaScript 引擎的一部分.也就是说,它必须与相应的嵌入器进行某些交互才能处理嵌入器管理的对象(例如 DOM 节点),这些对象的生命周期以某种方式与 JavaScript 对象相关联.

The garbage collector is part of the JavaScript engine. That said, it must have certain interactions with the respective embedder to deal with embedder-managed objects (e.g. DOM nodes) whose lifetime is tied to JavaScript objects in one way or another.

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) 以下哪一项具有更高的峰值内存使用率?

这两个片段(可能)具有相同的峰值内存使用量:它们都不允许同时访问由多次迭代分配的对象.

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.

如果你想阅读更多关于 V8 最近在做的与 GC 相关的工作,你可以在这里找到一系列的博客文章:https://v8.dev/blog/tags/memory

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

这篇关于JavaScript 垃圾收集器何时以及如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 16:38