Chrome中类型化数组的Javascript垃圾回收 [英] Javascript garbage collection of typed arrays in Chrome

查看:118
本文介绍了Chrome中类型化数组的Javascript垃圾回收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下javascript.我本来以为循环中的分配将允许垃圾回收启动以防止堆溢出.这样做在Firefox中是正确的,但是在Chrome(在OSX上进行了测试)中,该代码段经过多次迭代后使打开的标签页崩溃了.

Please consider the following javascript. I would have thought that the allocation in the loop would allow for garbaging collection to kick in preventing to overflow the heap. It rightly does so in Firefox, however in Chrome (tested on OSX) the snippet crashes the open tab after several iterations.

    for (var i = 0; i < 1024; ++i) {
            // Allocate a 16mb buffer
            var buffer = new Float32Array(1024*1024*4);

            // Try to explicitly mark it for free by either
            delete buffer;
            // or
            buffer = null;

            console.log(i);
    }

这个脚本本身并没有那么有用.但是我正在尝试优化我的Javascript应用程序,以使其使用更少的内存.因此,我想请您发表意见.这是Chrome中的错误吗?您是否知道在代码执行期间显式调用垃圾回收的任何变通办法(在FF和IE中它们似乎存在)?谢谢!

In itself this script it not all that useful. But I am trying to optimize my Javascript application in order for it to use less memory. Therefore I'd like your opinion. Is this a bug in Chrome? Do you know of any workarounds to explicitly call the garbage collection during code execution (in FF and IE they seem to exist)? Thanks!

Chrome浏览器检查器上似乎存在一个名为收集垃圾"的按钮.这是检查器时间轴"面板下部栏上的第7个按钮.这是否表示存在一种从Javascript调用GC的方法?毕竟,检查器的一部分不是用Java脚本编写的吗?

There appears to exist a button on the Chrome Inspector called "Collect Garbage". It is the 7th button on the lower bar on the "Timeline" panel of the Inspector. Doesn't this signify that a way exist to call GC from Javascript? After all, aren't parts of the Inspector written in Javascript?

推荐答案

这纯粹是推测,但是我想知道垃圾收集是否被推迟到运行循环中的当前项目完成执行为止.

This is pure speculation, but I wonder whether the garbage collection is being postponed until the the current item on the run loop finishes executing.

如果是这样,那么如果您将其成形为这样,那么也许会起作用:

If so, then perhaps it would work if you shaped it like this:

var i = 0;
function allocateArray() {
    var buffer = new Float32Array(1024*1024*4);
    if (++i < 1024) {
        setTimeout(allocateArray, 0); // Post the next call to the run loop
    }
}
allocateArray();

这篇关于Chrome中类型化数组的Javascript垃圾回收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆