问题
- js中在非use strict下使用eval会造成内存泄露吗?
- 怎么证明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>