作用域引起的内存泄漏:
<!DOCTYPE html>
<html>
<head>
<title>Scope Memery Leak</title>
</head>
<body>
<script type="module">
class MemoryLeak {
constructor() {
this.data = new Array(1024 * 1024 * 1024);
}
}
let leakObject = new MemoryLeak();
function createLeakFunction() {
let leakObjectRef = leakObject;
return function () {
if (leakObject) {
leakObject = null;
}
};
}
window.leakFunction = createLeakFunction();
</script>
</body>
</html>
JavaScript内存泄漏JavaScript是一门非常灵活的动态语言,和Java一样,JavaScript也具有动态内存回收机制(垃圾回收)。也就是说,如果一个对象没有任何人引用了,内存就会被自动释...