問題描述
什么是 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 垃圾回收?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!