什么是JavaScript垃圾收集? [英] What is JavaScript garbage collection?
问题描述
Eric Lippert写了一篇< a href =http://blogs.msdn.com/ericlippert/archive/2003/09/17/53038.aspx =noreferrer> 详细博客文章 稍等片刻(另外将它与 VBScript 比较)。更准确地说,他写了 JScript ,这是微软自己实施的ECMAScript虽然与JavaScript非常相似。我想你可以认为绝大多数的行为对于Internet Explorer的JavaScript引擎是一样的。当然,从浏览器到浏览器的实现都不尽相同,但我怀疑你可以采取一些常用的原则并将它们应用到其他浏览器中。
从该页面引用:
JScript使用非代码
标记和清除垃圾回收器。它
的工作原理是这样的:
$ ul
中的每个变量被称为清道夫。一个清道夫
可能指的是一个数字,一个对象,一个
字符串,无论如何。我们维护一个清单
的清单 - 当
进入范围时,变量被移动到scav列表中,当
超出范围时,变量从scav清单中移除。 / b>
垃圾
收集器现在运行。首先,它在每个对象,变量,
字符串等上放置
标记 - 所有内存由GC跟踪
。 (JScript在内部使用VARIANT
数据结构,而
在
结构中有很多额外的未使用位,所以我们只设置其中一个
。)
其次,它清除
清道夫上的标记和清道夫引用的传递闭包
。因此,如果一个
清道夫对象引用一个
nonscavenger对象,那么我们清除非清道夫上的
位,以及
所引用的所有内容。 (我是
,在
中使用closure这个词,与之前的
不同)。
此时我们知道仍然标记的所有
内存都被分配了
内存,这是任何范围内变量的任何
路径都无法达到的内存。所有
的这些对象都被指示为
将自己撕下,这会破坏任何循环引用的
。
垃圾收集的主要目的是让程序员不用担心他们创建和使用的对象的内存管理问题,当然,没有时常避免它 - 至少对垃圾收集工作至少有一个大概的想法总是有益的。
有几点需要注意。 Apple开发人员网站的 一些准则 。这里有两件重要的事: 我会想象这些实践应该适用于所有JavaScript引擎(在不同的浏览器中),但由于这是来自Apple网站,它们可能对Safari有一定的特殊性。 (也许有人可以澄清?) 希望有帮助。 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 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. Quoted from that page: 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. 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. There are a few particular points of which to be aware. The Apple developer site has some guidelines on the matter. Two important ones from there: I would imagine that the practices should apply to all JavaScript engines (in different browsers), though because this is from an Apple site, they may be somewhat specific to Safari. (Perhaps someone could clarify that?) Hope that helps. 这篇关于什么是JavaScript垃圾收集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ b $ b