Memory leak using window.eval() in Firefox?

核能气质少年 提交于 2019-12-07 16:32:36

问题


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>

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

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

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.

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.


回答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>


来源:https://stackoverflow.com/questions/39773622/memory-leak-using-window-eval-in-firefox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!