1. The attribute added to the DOM object is a reference to an object. Example:
var MyObject = {};
document.getElementById('myDiv').myProp = MyObject;
Solution:
Write in the window.onunload event: document.getElementById(' myDiv').myProp = null;
2. DOM objects and JS objects refer to each other. Example:
function Encapsulator(element) {
this.elementReference = element;
element.myProp = this;
}
new Encapsulator(document.getElementById('myDiv'));
Solution:
Write in the onunload event: document.getElementById('myDiv').myProp = null;
3. Use attachEvent to bind events to the DOM object. Example:
function doClick() {}
element.attachEvent("onclick", doClick);
Solution:
Write in the onunload event: element.detachEvent('onclick', doClick );
4. Execute appendChild from outside to inside. At this time, even if removeChild is called, it cannot be released. Example:
var parentDiv = document.createElement("div");
var childDiv = document.createElement("div");
document.body.appendChild(parentDiv);
parentDiv.appendChild (childDiv);
Solution:
Execute appendChild from inside to outside:
var parentDiv = document.createElement("div");
var childDiv = document.createElement("div");
parentDiv.appendChild(childDiv);
document.body.appendChild(parentDiv);
5. Repeatedly rewriting the same attribute will occupy a large amount of memory (but the memory will be released after closing IE). Example:
for(i = 0; i < 5000; i ) {
hostElement.text = "asdfasdfasdf";
}
This method is equivalent to defining 5000 attributes!
Solution:
In fact, there is no solution:P~~~Just try to avoid this situation when programming~~
Note:
1. The above information comes from Microsoft’s official MSDN site, link address:
http://msdn.microsoft.com/librar ... e_leak_patterns.asp
You can go to the above address to see detailed instructions, including examples and legends. It's just that my English is not very good and I can't understand it well. If I make any mistakes or have anything to add, please point it out.
2. For the first item, in fact, the writing method element.onclick = funcRef is also included, because this is also a reference to an object. It should be released when the page is onunloaded.
3. Regarding the third item, the English description of MSDN seems to say that even if detachEvent is called, the memory cannot be released, because the memory "LEAK" has been caused when attachEvent, but the situation will be better after detachEvent. I don't know if this is the case, please someone who is good at English can point it out.
4. In actual programming, the actual impact of these memory problems is not great, especially when used by customers, who will never notice it. However, these problems are always a worry for programmers-- - You always feel uncomfortable when you have such a BUG, right? If it can be solved, solve it. This is the best. In fact, in top JS source code sites like webfx.eae.net, I will see the above solution used for memory release management in their source code.
While studying jsvm, I found that js.lang.System defines the gc() method
System.gc = function ()
{
if (System.isIeBrowser())
{
CollectGarbage();
setTimeout("CollectGarbage();", 1) ;
}
}
CollectGarbage() is a unique memory release function of IE