什么是JavaScript垃圾收集?为了编写更好的代码,web程序员了解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 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完全不同。
其他回答
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完全不同。
垃圾收集(GC)是通过删除不再需要的对象来自动进行内存管理的一种形式。
任何处理内存的进程都遵循以下步骤:
1 -分配你需要的内存空间
2 -做一些处理
3 -释放这个内存空间
有两种主要的算法用于检测哪些对象不再需要。
引用计数垃圾收集:该算法将“一个对象不再需要”的定义简化为“一个对象没有其他对象引用它”,如果没有引用点,该对象将被删除
标记扫描算法:将每个对象连接到根源。任何对象都不连接根或其他对象。该对象将被移除。
目前大多数现代浏览器使用第二种算法。
什么是JavaScript垃圾收集?
检查这个
对于web程序员来说,了解JavaScript垃圾收集的重要一点是, 为了写出更好的代码?
在Javascript中,你不关心内存分配和释放。整个问题都需要Javascript解释器来解决。在Javascript中泄漏仍然是可能的,但它们是解释器的错误。如果你对这个话题感兴趣,你可以阅读更多 www.memorymanagement.org
引用类型不将对象直接存储到变量中 它被赋值了,下面例子中的对象变量,实际上没有 包含对象实例。相反,它持有指向的指针(或引用) 内存中对象存在的位置。
var object = new Object();
如果将一个引用类型变量赋值给另一个引用类型变量,则每个变量 获取指针的副本,两者仍然引用中相同的对象 内存。
var object1 = new Object();
var object2 = object1;
JavaScript是一种垃圾收集语言,所以实际上不需要这样做 使用引用类型时,要考虑内存分配问题。然而, 最好取消对不再需要的对象的引用,以便垃圾 收集器可以释放该内存。最好的方法是设置 对象变量为null。
var object1 = new Object();
// do something
object1 = null; // dereference
在使用数百万个对象的大型应用程序中,取消引用对象尤其重要。
摘自《面向对象的JavaScript原则》——NICHOLAS C. ZAKAS
当涉及到DOM对象时,要注意循环引用:
JavaScript中的内存泄漏模式
请记住,只有在没有对对象的活动引用时才能回收内存。这是闭包和事件处理程序的一个常见陷阱,因为一些JS引擎不会检查内部函数中实际引用了哪些变量,而只是保留封闭函数的所有局部变量。
这里有一个简单的例子:
function init() {
var bigString = new Array(1000).join('xxx');
var foo = document.getElementById('foo');
foo.onclick = function() {
// this might create a closure over `bigString`,
// even if `bigString` isn't referenced anywhere!
};
}
只要事件处理程序存在,简单的JS实现就不能收集bigString。有几种方法可以解决这个问题,例如在init()末尾设置bigString = null (delete对局部变量和函数参数不起作用:delete将从对象中删除属性,并且变量对象不可访问-如果您试图删除局部变量,ES5在严格模式下甚至会抛出ReferenceError !)
如果您关心内存消耗,我建议尽可能避免不必要的闭包。
推荐文章
- AngularJS:工厂和服务?
- js:将一个组件包装成另一个组件
- 父ng-repeat从子ng-repeat的访问索引
- JSHint和jQuery: '$'没有定义
- 模仿JavaScript中的集合?
- 用JavaScript验证电话号码
- 如何在HTML5中改变视频的播放速度?
- 谷歌地图API v3:我可以setZoom后fitBounds?
- ES6/2015中的null安全属性访问(和条件赋值)
- 与push()相反;
- JS字符串“+”vs concat方法
- AngularJS使用ng-class切换类
- 访问Handlebars.js每次循环范围之外的变量
- 如何用JavaScript截屏一个div ?
- 如何为其他域设置cookie