Memory leak and garbage collection mechanism

What is a memory leak?

程序的运行需要内存,只要程序提出要求,操作系统或者运行是就必须供给内存。对于持续运行的服务进程,必须及时释放内存,否则,内存占用越来越高,轻则影响系统性能,重则导致进程崩溃。不再用到的内存,没有及时释放,就叫做内存泄漏。

Some languages ​​(such as C language) must manually release memory, and the programmer is responsible for memory management. This is very troublesome, so most languages ​​provide automatic memory management to reduce the burden on programmers, which is called "garbage collection mechanism".

Causes of common memory leaks

Memory leak caused by global variables

  function leaks(){
    
      
    leak = 'xxxxxx';//leak 成为一个全局变量,不会被回收
}

Memory leak caused by closure

var leaks = (function(){
    
      
    var leak = 'xxxxxx';// 被闭包所引用,不会被回收
    return function(){
    
    
        console.log(leak);
    }
   })()

Memory leak caused by uncleared events when dom is cleared or deleted

<div id="container">
</div>
 $('#container').bind('click', function(){
    
    
    console.log('click');
}).remove();  
// zepto 和原生 js下,#container dom 元素,还在内存里jquery 的 empty和 remove会帮助开发者避免这个问题  
<div id="container">  
</div>
$('#container').bind('click', function(){
    
    
    console.log('click');
}).

Clear the event, you can remove it from the memory

Principle of javascript garbage collection mechanism

To solve memory leaks, the garbage collection mechanism will periodically (periodically) find out the memory (variables) that is no longer used, and then release its memory. There are two methods for garbage collection mechanisms commonly used by major browsers: mark removal and reference counting.

Necessity : Since strings, objects, and arrays have no fixed size, only when their size is known can they be dynamically allocated. Every time a JavaScript program creates a string, array, or object, the interpreter must allocate memory to store that entity. As long as the memory is dynamically allocated like this, the memory must eventually be released so that they can be reused, otherwise, the JavaScript interpreter will consume all available memory in the system and cause the system to crash.

1. Mark removal

This is the most commonly used garbage collection method in JavaScript. When the variable enters the execution environment, mark the variable as "enter environment". Most browsers perform garbage collection in this way. When the variable enters the execution environment (the variable is declared in the function), the garbage collector marks it as "entering the environment", and when the variable leaves the environment (the function execution ends) Marked as "Leave the Environment", the variables remaining after leaving the environment are the variables that need to be deleted. The marking method is variable, it can be the inversion of a special bit or maintaining a list, etc.
The garbage collector adds tags to all variables in the memory, and then removes the tags of the variables in the environment and the variables referenced by the variables in the environment. The marked variables added after this are the variables that need to be recycled, because the variables in the environment can no longer access these variables.

2. Reference counting

Another less common garbage collection strategy is reference counting. This method often causes memory leaks, and lower versions of IE use this method. The mechanism is to track the number of references to a value. When a variable is declared and a reference type is assigned to the variable, the number of references to the value is increased by 1, and when the variable points to another one, the number of references to the value is reduced by one. When the number of references of the value is 0, it will be recycled.

Guess you like

Origin blog.csdn.net/zzDAYTOY/article/details/108568681