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

How to deal with JS memory leaks in versions before IE9

php中世界最好的语言
Release: 2018-06-01 17:24:51
Original
1299 people have browsed it

This time I will show you how to deal with memory leaks in JS in versions before IE9, and what are the precautions for dealing with memory leaks in JS in versions before IE9. The following is a practical case, 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.

You can solve the problem by slightly modifying the above code:

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 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:

How to use vue.extend to implement the alert modal box pop-up component

How to use vue pop-up Window message component

The above is the detailed content of How to deal with 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