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 aHTML 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); }; }
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; }
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!