什么是 JavaScript 垃圾回收? [英] What is JavaScript garbage collection?

查看:18
本文介绍了什么是 JavaScript 垃圾回收?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是 JavaScript 垃圾回收?为了编写更好的代码,对于 Web 程序员来说,了解 JavaScript 垃圾收集有什么重要意义?

What is JavaScript garbage collection? What's important for a web programmer to understand about JavaScript garbage collection, in order to write better code?

推荐答案

Eric Lippert 写了一个 详细的博客文章不久前关于这个主题(另外将它与 VBScript 进行比较).更准确地说,他写的是 JScript,这是微软自己实现的ECMAScript,虽然与 JavaScript 非常相似.我想您可以假设 Internet Explorer 的 JavaScript 引擎的绝大多数行为都是相同的.当然,实现会因浏览器而异,但我怀疑您可以采用一些通用原则并将它们应用于其他浏览器.

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 使用非分代标记和清除垃圾收集器.它像这样工作:

JScript uses a nongenerational mark-and-sweep garbage collector. It works like this:

  • 范围内"的每个变量被称为清道夫".清道夫可以指一个数字、一个对象、一个字符串,随便.我们维护一个列表清道夫——变量被移动当他们来的时候进入 scav 名单进入范围并退出 scav 列表时他们超出了范围.

  • 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.

时不时的垃圾收集器运行.首先它放了一个在每个对象、变量上标记",字符串等 - 跟踪的所有内存由 GC.(JScript 使用 VARIANT内部和那里的数据结构有很多额外的未使用的位那个结构,所以我们只设置一个他们.)

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.)

其次,它清除了标记清道夫和传递闭包清道夫参考.所以如果一个scavenger 对象引用 anonscavenger 对象然后我们清除非清道夫上的位,以及它所指的一切.(我是在 a 中使用闭包"这个词和我之前的感觉不同发布.)

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.

垃圾回收的主要目的是让程序员担心他们创建和使用的对象的内存管理,当然有时也无法避免 - 拥有它总是有益的至少大致了解垃圾收集的工作原理.

The main purpose of garbage collection is to allow the programmer not to worry about memory management of the objects they create and use, though of course there's no avoiding it sometimes - it is always beneficial to have at least a rough idea of how garbage collection works.

历史记录:答案的早期版本对 delete 运算符的引用不正确.在 JavaScript delete 运算符中删除对象的属性,与 C/C++ 中的 delete 完全不同.

Historical note: an earlier revision of the answer had an incorrect reference to the delete operator. In JavaScript the delete operator removes a property from an object, and is wholly different to delete in C/C++.

这篇关于什么是 JavaScript 垃圾回收?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆