Eric Lippert wrote a detailed blog post about this subject a while back (additionally comparing it to VBScript ).
(埃里克·利珀特(Eric Lippert)不久前就此主题撰写了一篇详细的博客文章 (另外将其与VBScript进行了比较)。)
More accurately, he wrote about JScript , which is Microsoft's own implementation of ECMAScript, although very similar to JavaScript.(更准确地说,他写了有关JScript的信息,尽管它非常类似于JavaScript,但它是Microsoft自己的ECMAScript实现。)
I would imagine that you can assume the vast majority of behaviour would be the same for the JavaScript engine of Internet Explorer.(我可以想象,您可以假设绝大多数行为对于Internet Explorer的JavaScript引擎都是相同的。)
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.
(JScript使用了非世代的标记清除垃圾收集器。)
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.(我们维护了一个清除剂列表-变量在进入作用域时将移至scav列表中,而当变量超出作用域时将其移至scav列表中。)
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.(首先,它在每个对象,变量,字符串等(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.)((JScript在内部使用VARIANT数据结构,并且该结构中有许多额外的未使用位,因此我们仅设置其中之一。))
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.)((我使用“ closure”一词的含义与我以前的帖子不同。))
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.(Apple开发人员网站对此有一些指导原则 。)
Two important ones from there:(从那里两个重要的:)
- Use delete statements.
(使用删除语句。)
Whenever you create an object using a new statement, pair it with a delete statement.(每当使用新语句创建对象时,都将其与delete语句配对。)
This ensures that all of the memory associated with the object, including its property name, is available for garbage collection.(这样可以确保与对象关联的所有内存(包括其属性名称)都可用于垃圾回收。)
The delete statement is discussed more in “Freeing Objects.”(在“释放对象”中将详细讨论delete语句。)
- Use the var keyword.
(使用var关键字。)
Any variable created without the var keyword is created at the global scope and is never eligible for garbage collection, presenting the opportunity for a memory leak.(任何不带var关键字创建的变量都将在全局范围内创建,并且永远不会进行垃圾回收,这会导致内存泄漏。)
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.
(我想这些做法应该适用于所有JavaScript引擎(在不同的浏览器中),尽管由于它来自Apple网站,所以它们可能特定于Safari。)
(Perhaps someone could clarify that?)((也许有人可以澄清吗?))
Hope that helps.
(希望能有所帮助。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…