什么是JavaScript垃圾收集?为了编写更好的代码,web程序员了解JavaScript垃圾收集的重要方面是什么?
当前回答
当涉及到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 !)
如果您关心内存消耗,我建议尽可能避免不必要的闭包。
其他回答
当涉及到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 !)
如果您关心内存消耗,我建议尽可能避免不必要的闭包。
在计算机科学中,垃圾收集(GC)是自动内存管理的一种形式。垃圾收集器,或者仅仅是垃圾收集器,尝试回收垃圾或对象所使用的内存,这些对象将永远不会被应用程序再次访问或更改。”
所有JavaScript引擎都有自己的垃圾收集器,它们可能有所不同。大多数时候你不需要和他们打交道,因为他们只是在做他们应该做的事情。
写出更好的代码主要取决于你对编程原理、语言和特定实现的了解程度。
在javascript中,垃圾收集是不确定的,对象何时会被清除,或者是否会被清除。这适用于强引用的对象。强引用对象不受垃圾回收的影响。
在ES12之后,可以执行以下实现来检查对象何时被垃圾收集。
要了解更多关于javascript垃圾收集的知识,你可以使用ES12之后可用的终结器。
let a = new Array(200).fill(true);
构造定稿器
const cleanup = new FinalizationRegistry(key => {
// your code here
});
cleanup.register(a, 'wewew');
对象'a'现在不可达,终结器回调将在垃圾收集后发生
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完全不同。
据我所知,JavaScript的对象在没有对对象的引用时周期性地进行垃圾收集。这是自动发生的事情,但如果你想了解更多关于它是如何工作的,在c++级别,看一看WebKit或V8源代码是有意义的
Typically you don't need to think about it, however, in older browsers, like IE 5.5 and early versions of IE 6, and perhaps current versions, closures would create circular references that when unchecked would end up eating up memory. In the particular case that I mean about closures, it was when you added a JavaScript reference to a dom object, and an object to a DOM object that referred back to the JavaScript object. Basically it could never be collected, and would eventually cause the OS to become unstable in test apps that looped to create crashes. In practice these leaks are usually small, but to keep your code clean you should delete the JavaScript reference to the DOM object.
通常,使用delete关键字立即取消对JSON数据等大对象的引用是一个好主意,特别是在移动web开发中。这将导致GC的下一次扫描删除该对象并释放其内存。
推荐文章
- 如何让一个按钮将我的页面重定向到另一个页面?
- 如何让元素被点击(对于整个文档)?
- 我如何检查如果一个变量是JavaScript字符串?
- 如何检测如果多个键被按下一次使用JavaScript?
- 如何通过history. pushstate获得历史变化的通知?
- 使用jQuery改变输入字段的类型
- 在JavaScript中,什么相当于Java的Thread.sleep() ?
- 使用jQuery以像素为整数填充或边距值
- 检查是否选择了jQuery选项,如果没有选择默认值
- Next.js React应用中没有定义Window
- 如何重置笑话模拟函数调用计数之前,每次测试
- 如何强制一个功能React组件渲染?
- 在javascript中从平面数组构建树数组
- 将Dropzone.js与其他字段集成到现有的HTML表单中
- 如何在AngularJS中观察路由变化?