Out of memory

  • A program execution error
  • When a program needs more memory to run than the computer has left, the program crashes and throws an out-of-memory error

Memory leaks

  • The memory is occupied and not released in time
  • The more memory leaks, the less memory the computer has left, and the more likely memory leaks are
  • Common memory leaks:
  • Unexpected global variables
  • The timer or callback function was not cleaned up in time
  • closure
function x(){
a = 10;
//或者this.a = 10;
//或者window.a = 10;
}
x();
// An unexpected global variable was generated
Copy the code
var intervald = setInterval(function (){
console.log(The '-');
},1000);
// Clear the timer
clearInterval(intervalId);
-----------------------------------------
var element = document.getElementById('button');
function onClick(event) {
element.innerHtml = 'text';
}
element.addEventListener('click', onClick);
element.removeEventListener('click', onClick);
element.parentNode.removeChild(element);
Copy the code
function a(){
var x = 10;
function b(){
return x;
}
return b;
}
var f = a();
f();
// Set the internal function object to garbage
f = null;
Copy the code