Memory leak using window.eval() in Firefox

2019-05-14 07:33发布

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条回答
一纸荒年 Trace。
2楼-- · 2019-05-14 08:34

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>
查看更多
登录 后发表回答