在Firefox中使用window.eval()进行内存泄漏? [英] Memory leak using window.eval() in Firefox?

查看:74
本文介绍了在Firefox中使用window.eval()进行内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的测试用例,我调用 window.evaluate 函数。

I have a simple test case where I call the window.evaluate function.

<html>
<head></head>
<body>
    <script>
        function test() {
            // do something important
        }   
        setInterval(
            function() {
                window.eval("test();");
            }, 
            100
        );
    </script>
</body>
</html>

现在,当我在Firefox,Chrome和Edge中打开此页面并分析内存时,我可以看到不同的内容结果。请注意,我正在使用Windows 10 Pro 64b。

Now when I open this page within Firefox, Chrome and Edge and profile the memory, I can see different results. Note that I'm using Windows 10 Pro 64b.

         at start      after cca 9 hours   browser version
         -------------------------------------------------
Edge:    0.28MB        0.28MB              38.14393.0.0
Chrome:  1.70MB        1.90MB              53.0.2785.116
Firefox: 0.25MB        115.3MB             49.0.1

Firefox中的堆大小增长 - 从0.25 MB到115 MB

There is a huge heap size growth in Firefox - from 0.25 MB to 115MB.

正如@charlietfl在这个问题下面的评论中提到的,我也做了相同的测试而没有 window.eval 函数。所以我只是在间隔回调中调用 test()

As @charlietfl mention in a comment below this question I also did the same test without the window.eval function. So I just call test() within the interval callback.

         at start      after cca 9 hours   browser version
         -------------------------------------------------
Edge:    0.27MB        0.27MB              38.14393.0.0
Chrome:  1.60MB        1.60MB              53.0.2785.116
Firefox: 0.19MB        0.19MB              49.0.1

如您所见,任何堆大小都没有改变。

As you can see, any heap size didn't change at all.

所以我问这个测试javascript代码本身是错误的还是Firefox在使用 window.evaluate function。

So I'm asking if this testing javascript code is wrong on its own or the Firefox is buggy when using the window.evaluate function.

推荐答案

是的,存在49.0.1中的内存泄漏。功能可以在没有内存问题的情况下工作的替代方法。

So yes, the memory leak in 49.0.1 exists. An Alternative method that should work without the memory issues is Function.

<html>
<head></head>
<body>
    <script>
        function test() {
            console.log("running test");
        }   
        setInterval(
            function() {
                 (new Function( 'return ' + 'test()' ) )();
            }, 
            100
        );
    </script>
</body>
</html>

这篇关于在Firefox中使用window.eval()进行内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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