什么是JavaScript垃圾收集?为了编写更好的代码,web程序员了解JavaScript垃圾收集的重要方面是什么?


当前回答

这句话摘自博客

DOM组件是“垃圾收集”的,JScript组件也是如此,这意味着如果您在任何一个组件中创建了一个对象,然后失去了对该对象的跟踪,它最终将被清除。

例如:

function makeABigObject() {
var bigArray = new Array(20000);
}

When you call that function, the JScript component creates an object (named bigArray) that is accessible within the function. As soon as the function returns, though, you "lose track" of bigArray because there's no way to refer to it anymore. Well, the JScript component realizes that you've lost track of it, and so bigArray is cleaned up--its memory is reclaimed. The same sort of thing works in the DOM component. If you say document.createElement('div'), or something similar, then the DOM component creates an object for you. Once you lose track of that object somehow, the DOM component will clean up the related.

其他回答

在javascript中,垃圾收集是不确定的,对象何时会被清除,或者是否会被清除。这适用于强引用的对象。强引用对象不受垃圾回收的影响。

在ES12之后,可以执行以下实现来检查对象何时被垃圾收集。

要了解更多关于javascript垃圾收集的知识,你可以使用ES12之后可用的终结器。

let a = new Array(200).fill(true);

构造定稿器

const cleanup = new FinalizationRegistry(key => {
  // your code here
});

cleanup.register(a, 'wewew');

对象'a'现在不可达,终结器回调将在垃圾收集后发生

什么是JavaScript垃圾收集?

检查这个

对于web程序员来说,了解JavaScript垃圾收集的重要一点是, 为了写出更好的代码?

在Javascript中,你不关心内存分配和释放。整个问题都需要Javascript解释器来解决。在Javascript中泄漏仍然是可能的,但它们是解释器的错误。如果你对这个话题感兴趣,你可以阅读更多 www.memorymanagement.org

这句话摘自博客

DOM组件是“垃圾收集”的,JScript组件也是如此,这意味着如果您在任何一个组件中创建了一个对象,然后失去了对该对象的跟踪,它最终将被清除。

例如:

function makeABigObject() {
var bigArray = new Array(20000);
}

When you call that function, the JScript component creates an object (named bigArray) that is accessible within the function. As soon as the function returns, though, you "lose track" of bigArray because there's no way to refer to it anymore. Well, the JScript component realizes that you've lost track of it, and so bigArray is cleaned up--its memory is reclaimed. The same sort of thing works in the DOM component. If you say document.createElement('div'), or something similar, then the DOM component creates an object for you. Once you lose track of that object somehow, the DOM component will clean up the related.

垃圾收集(GC)是通过删除不再需要的对象来自动进行内存管理的一种形式。

任何处理内存的进程都遵循以下步骤:

1 -分配你需要的内存空间

2 -做一些处理

3 -释放这个内存空间

有两种主要的算法用于检测哪些对象不再需要。

引用计数垃圾收集:该算法将“一个对象不再需要”的定义简化为“一个对象没有其他对象引用它”,如果没有引用点,该对象将被删除

标记扫描算法:将每个对象连接到根源。任何对象都不连接根或其他对象。该对象将被移除。

目前大多数现代浏览器使用第二种算法。

Eric Lippert wrote a detailed blog post about this subject a while back (additionally comparing it to VBScript). More accurately, he wrote about JScript, which is Microsoft's own implementation of ECMAScript, although very similar to JavaScript. I would imagine that you can assume the vast majority of behaviour would be the same for the JavaScript engine of Internet Explorer. Of course, the implementation will vary from browser to browser, though I suspect you could take a number of the common principles and apply them to other browsers.

引自该页:

JScript uses a nongenerational mark-and-sweep garbage collector. It works like this: Every variable which is "in scope" is called a "scavenger". A scavenger may refer to a number, an object, a string, whatever. We maintain a list of scavengers -- variables are moved on to the scav list when they come into scope and off the scav list when they go out of scope. Every now and then the garbage collector runs. First it puts a "mark" on every object, variable, string, etc – all the memory tracked by the GC. (JScript uses the VARIANT data structure internally and there are plenty of extra unused bits in that structure, so we just set one of them.) Second, it clears the mark on the scavengers and the transitive closure of scavenger references. So if a scavenger object references a nonscavenger object then we clear the bits on the nonscavenger, and on everything that it refers to. (I am using the word "closure" in a different sense than in my earlier post.) At this point we know that all the memory still marked is allocated memory which cannot be reached by any path from any in-scope variable. All of those objects are instructed to tear themselves down, which destroys any circular references.

垃圾收集的主要目的是让程序员不必担心他们创建和使用的对象的内存管理问题,当然有时无法避免——至少对垃圾收集的工作方式有一个粗略的了解总是有益的。

历史注释:答案的早期修订对delete操作符的引用不正确。在JavaScript中,delete操作符从对象中删除一个属性,这与C/ c++中的delete完全不同。