我有一个简单的测试用例,我在其中调用了 window.evaluate
函数。
<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。
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 到 115MB。
正如@charlietfl 在这个问题下方的评论中提到的,我也在没有 window.eval
函数的情况下进行了相同的测试。所以我只是在间隔回调中调用 test()
。
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
如您所见,任何堆大小都没有发生任何变化。
所以我想问的是,这个测试 javascript 代码本身是错误的,还是 Firefox 在使用 window.evaluate
函数时出现错误。
最佳答案
所以是的,49.0.1 中存在内存泄漏。应该可以在没有内存问题的情况下工作的替代方法是 Function。
<html>
<head></head>
<body>
<script>
function test() {
console.log("running test");
}
setInterval(
function() {
(new Function( 'return ' + 'test()' ) )();
},
100
);
</script>
</body>
</html>
关于javascript - 在 Firefox 中使用 window.eval() 内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39773622/