什么是 JavaScript 垃圾回收?

2023-05-13前端开发问题
7

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

问题描述

限时送ChatGPT账号..

什么是 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 垃圾回收?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455

Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
2024-04-20 前端开发问题
5

CoffeeScript 总是以匿名函数返回
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20 前端开发问题
13