Home > Web Front-end > JS Tutorial > How to avoid JS memory leaks in versions before IE9

How to avoid JS memory leaks in versions before IE9

php中世界最好的语言
Release: 2018-05-15 10:25:42
Original
1268 people have browsed it

This time I will bring you how to avoid memory leaks of JS in versions before IE9, and how to avoid memory leaks of JS in versions before IE9. What are the precautions?. Here are practical cases. Let’s take a look. .

Versions before IE9 use different garbage collection routines for JScript objects and COM objects (COM objects use a "reference counting" collection strategy), so closures will cause some special problems in these versions of IE. Specifically, if a HTML element is stored in the scope of the closure, it means that the element cannot be destroyed.
Look at the following example:

function assignHandler() {
  var elem = document.getElementById('elem_id');
  elem.onclick = function(evt) {
    alert(elem.id);
  };
}
Copy after login

The above code creates a closure as the elem element Event handler program, and this closure creates a circular reference. Since the anonymous function saves a reference to the active object of assignHandler(), it will be impossible to reduce the number of elem references. As long as the anonymous function exists, the reference number of elem is at least 1, so the memory it occupies will never be recycled.

The above code can be solved by slightly modifying it:

function assignHandler() {
  var elem = document.getElementById('elem_id');
  var elem_id = elem.id;
  elem.onclick = function(evt) {
    alert(elem_id);
  };
  elem = null;
}
Copy after login

Eliminate the problem by saving a copy of elem.id in a variable and referencing the variable in the closure. Circular references. But just doing this step still cannot solve the memory leak problem.

"The closure refers to the entire active object containing the function, which contains elem. Even if the closure does not directly reference elem, a reference will still be saved in the active object containing the function. Therefore, it is necessary Set elem to null. This will dereference the DOM object, smoothly reduce its number of references, and ensure normal recycling of the memory it occupies."

I believe you have already read the case in this article After mastering the method, please pay attention to other related articles on the php Chinese website for more exciting content!

Recommended reading:

Detailed explanation of the steps to implement dynamic and static paging in layui

vue jquery lodash top floating fixed function when sliding Implementation details

The above is the detailed content of How to avoid JS memory leaks in versions before IE9. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template