js中在非use strict下怎么证明eval会造成内存泄露?

问题

  1. js中在非use strict下使用eval会造成内存泄露吗?
  2. 怎么证明eval会造成内存泄露

代码及演示

下面的代码会点击evalClosures被回收掉

代码出处链接


<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>


<p>
<button onclick="largeClosures()">largeClosures</button></p>
<p><button onclick="smallClosures()">smallClosures</button></p>
<p><button onclick="evalClosures()">evalClosures</button></p>
<p><button onclick="clearClosures()">clearClosures</button></p>


<script >

var closures = [];

function createLargeClosure() {
    var largeStr = new Array(1000000).join('x');
    return function lC() {
        return largeStr;
    };
}

function createSmallClosure() {
    var smallStr = 'x';
    var largeStr = new Array(1000000).join('x');
    return function sC() {
        return smallStr;
    };
}

function createEvalClosure() {
    var smallStr = 'x';
    var largeStr = new Array(1000000).join('x');
    return function eC() {
        eval('');
        return smallStr;
    };
}

function largeClosures() {
    closures.push(createLargeClosure());
}

function smallClosures() {
    closures.push(createSmallClosure());

}

function evalClosures() {
    closures.push(createEvalClosure());
    
}

function clearClosures() {
    closures = []
}


</script>
</body>
</html>

3121.gif

阅读 264
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Microsoft
子站问答
访问
宣传栏