问题描述
我想触发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?
镇上的所有人都说删除引用将达到目的.有人说,擦拭对象是一种额外的保证(示例).因此,我编写了一个脚本,尝试了本书中的所有技巧,但令我惊讶的是,在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).
使用伪代码,测试过程如下.
-
创建一个容器,一个数组,将容纳某种对象.我们在这里将其称为
Bertil
容器.
其中的每个对象(作为Bertil中的元素)都应将其自己的array-container声明为属性.该数组将容纳很多字节.我们将Bertil的任何元素称为对象 Joshua
.每个约书亚的字节数组都称为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
[对象数组]-> Joshua
[对象]-> Smith
[字节数组]->未命名的[字节].
Bertil
[Array of objects] -> Joshua
[Object] -> Smith
[Array of bytes] -> Unnamed [Bytes].
当我们将可用内存弄得一团糟时,请闲逛一两秒钟,然后执行以下任一破坏算法":
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上放置一个删除操作数.
4a. Throw a delete operand on the main object container, Bertil.
4b.在该容器中的每个对象上扔一个删除操作数,杀死每个活着的约书亚.
4b. Throw a delete operand on each and every object in that container, kill every Joshua alive.
4c.在每个字节数组Smith上都添加一个删除操作数.
4c. Throw a delete operand on each and every array of bytes, the Smiths.
4d.为每个约书亚分配NULL.
4d. Assign NULL to every Joshua.
4e.为每个约书亚分配UNDEFINED.
4e. Assign UNDEFINED to every Joshua.
4f.手动删除任何约书亚记中的每个字节.
4f. Manually delete each and every byte that any Joshua holds.
4克.按照工作顺序进行上述所有操作.
4g. Do all of the above in a working order.
那发生了什么??如果 4a 和 4b ,没有浏览器的垃圾收集器(GC)被踢进来.如果 4c到4e ,Firefox确实加入其中并展示了一些概念证明.此刻很快就恢复了记忆.在某些变量上使用当前硬编码的默认值作为测试配置的情况下,情况 4f 和 4e 导致Chrome挂起,因此我无法在此处得出任何结论.您可以自由使用自己的变量进行测试,链接将很快发布. IE在4f和4e案中幸存下来,但他的GC照常死了.出乎意料的是,Firefox幸存下来,但没有通过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分钟并不能解决问题.重新加载整个页面会导致内存占用量增加一倍.
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.
我的结论是,我必须在代码中犯了一个可怕的错误,或者您的问题的答案是:不,我们不能触发GC.每当我们尝试这样做时,我们都会受到严厉的惩罚,我们应该将头埋在沙子里.请鼓励我继续,自己尝试这些测试用例.看一下代码中的注释细节.另外,下载页面并重写脚本,看看是否可以以更适当的方式触发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=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
这篇关于我可以触发JavaScript的垃圾回收吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!