問題描述
我想觸發(fā) JavaScript 垃圾回收.是否可以?我為什么要或不想這樣做?
I want to trigger JavaScript garbage collection. Is it possible? Why would I want to, or not want to, do this?
推薦答案
我踏上了一小段旅程,想要回答你的一個問題:有可能嗎?
I went out on a small journey to seek an answer to one of your questions: Is it possible?
全城的人都在說刪除引用就行了.有人說擦除對象是額外的保證(示例).所以我寫了一個腳本來嘗試書中的每一個技巧,我驚訝地發(fā)現(xiàn)在 Chrome (22.0.1229.79) 和 IE (9.0.8112.16421) 中,垃圾收集似乎不起作用.Firefox (15.0.1) 的管理沒有任何重大缺陷,除了一個(參見下面的案例 4f).
People all over town are saying that deleting the references will do the trick. Some people say that wiping the object is an extra guarantee (example). So I wrote a script that will try every trick in the book, and I was astonished to see that in Chrome (22.0.1229.79) and IE (9.0.8112.16421), garbage collection doesn't even seem to work. Firefox (15.0.1) managed without any major drawbacks apart from one (see case 4f down below).
在偽代碼中,測試是這樣的.
創(chuàng)建一個容器,一個數(shù)組,用于保存某種對象.我們將在此處將此容器稱為
Bertil
.
其中的每個對象,作為 Bertil 中的一個元素,都應將其自己的數(shù)組容器聲明為屬性.該數(shù)組將保存大量字節(jié).我們將調(diào)用 Bertil 的任何元素,對象,Joshua
.每個 Joshua 的字節(jié)數(shù)組將被稱為 Smith
.
Each and every object therein, as an element in Bertil, shall have his own array-container declared as a property. This array will hold a whole lot of bytes. We'll call any one of Bertil's elements, the object, Joshua
. Each Joshua's byte array will be called Smith
.
這里有一張思維導圖供你參考:
Here's a mind map for you to lean back on:
Bertil
[對象數(shù)組] -> Joshua
[對象] -> Smith
[字節(jié)數(shù)組] -> 未命名 [字節(jié)].
Bertil
[Array of objects] -> Joshua
[Object] -> Smith
[Array of bytes] -> Unnamed [Bytes].
當我們把可用內(nèi)存弄得一團糟時,停留一兩秒,然后執(zhí)行以下任一破壞算法":
When we've made a mess out of our available memory, hang around for a sec or two and then execute any one of the following "destruction algorithms":
4a.在主對象容器 Bertil 上拋出一個刪除操作數(shù).
4a. Throw a delete operand on the main object container, Bertil.
4b.對該容器中的每個對象都拋出一個刪除操作數(shù),殺死所有活著的 Joshua.
4b. Throw a delete operand on each and every object in that container, kill every Joshua alive.
4c.對每個字節(jié)數(shù)組(Smiths)拋出一個刪除操作數(shù).
4c. Throw a delete operand on each and every array of bytes, the Smiths.
4d.將 NULL 分配給每個 Joshua.
4d. Assign NULL to every Joshua.
4e.將 UNDEFINED 分配給每個 Joshua.
4e. Assign UNDEFINED to every Joshua.
4f.手動刪除任何 Joshua 保存的每個字節(jié).
4f. Manually delete each and every byte that any Joshua holds.
4g.按正常順序執(zhí)行上述所有操作.
4g. Do all of the above in a working order.
那么發(fā)生了什么?在 4a 和 4b 的情況下,沒有瀏覽器的垃圾收集器 (GC) 啟動.在 4c 的情況下到 4e,F(xiàn)irefox 確實啟動并展示了一些概念證明.記憶在一分鐘內(nèi)很快被回收.使用當前硬編碼的一些變量作為測試配置的默認值,案例 4f 和 4e 導致 Chrome 掛起,所以我不能在那里得出任何結(jié)論.您可以自由地使用自己的變量進行自己的測試,鏈接將很快發(fā)布.IE 在 4f 和 4e 病例中幸存下來,但他的 GC 像往常一樣死了.出乎意料的是,F(xiàn)irefox 幸存下來但沒有通過 4f.Firefox 幸存下來并通過了 4g.
So what happened? In case 4a and 4b, no browser's garbage collector (GC) kicked in. In case 4c to 4e, Firefox did kick in and displayed some proof of concept. Memory was reclaimed shortly within the minute. With current hardcoded default values on some of the variables used as test configuration, case 4f and 4e caused Chrome to hang, so I can't draw any conclusions there. You are free to do your own testing with your own variables, links will be posted soon. IE survived case 4f and 4e but his GC was dead as usual. Unexpectedly, Firefox survived but didn't pass 4f. Firefox survived and passed 4g.
在瀏覽器的 GC 未能啟動的所有情況下,等待至少 10 分鐘并不能解決問題.并且重新加載整個頁面會導致內(nèi)存占用翻倍.
In all of the cases when a browser's GC failed to kick in, waiting around for at least 10 minutes didn't solve the problem. And reloading the entire page caused the memory footprint to double.
我的結(jié)論是我一定在代碼中犯了一個可怕的錯誤,或者你的問題的答案是:不,我們不能觸發(fā) GC.每當我們試圖這樣做時,我們都會受到嚴厲的懲罰,我們應該把頭埋在沙子里.請我鼓勵你繼續(xù),自己嘗試這些測試用例.看看代碼中對細節(jié)的評論.另外,下載頁面并重寫腳本,看看是否可以以更合適的方式觸發(fā) GC.我確實失敗了,我一輩子都不能相信 Chrome 和 IE 沒有工作的垃圾收集器.
My conclusion is that I must have made a horrible error in the code or the answer to your question is: No we can't trigger the GC. Whenever we try to do so we will be punished severely and we should stick our heads in the sand. Please I encourage you to go ahead, try these test cases on your own. Have a look in the code were comment on the details. Also, download the page and rewrite the script and see if you can trigger the GC in a more proper way. I sure failed and I can't for the life of me believe that Chrome and IE doesn't have a working garbage collector.
http://martinandersson.com/dev/gc_test/?case=1
http://martinandersson.com/dev/gc_test/?case=2
http://martinandersson.com/dev/gc_test/?case=3
http://martinandersson.com/dev/gc_test/?case=4
http://martinandersson.com/dev/gc_test/?case=5
http://martinandersson.com/dev/gc_test/?case=6
http://martinandersson.com/dev/gc_test/?case=7
這篇關于我可以觸發(fā) JavaScript 的垃圾回收嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!