1. the attribute added to the DOM object is an object reference. Example:
Var MyObject = {};
Document. getElementById ('mydiv '). myProp = MyObject;
Solution:
In the window. onunload Event, write: document. getElementById ('mydiv '). myProp = null;
2. DOM objects and JS objects reference each other. Example:
Function Encapsulator (element ){
This. elementReference = element;
Element. myProp = this;
}
New Encapsulator (document. getElementById ('mydiv '));
Solution:
In the onunload Event, write: document. getElementById ('mydiv '). myProp = null;
3. Bind events to DOM objects using attachEvent. Example:
Function doClick (){}
Element. attachEvent ("onclick", doClick );
Solution:
In the onunload Event, write: element. detachEvent ('onclick', doClick );
4. Execute appendChild from the external to the inner. 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 the inside out:
Var parentDiv = document. createElement ("div ");
Var childDiv = document. createElement ("div ");
ParentDiv. appendChild (childDiv );
Document. body. appendChild (parentDiv );
5. Repeated rewriting of the same attribute will cause a large amount of memory usage (but the memory will be released after IE is disabled ). Example:
For (I = 0; I <5000; I ++ ){
HostElement. text = "asdfasdfasdf ";
}
This method is equivalent to defining 5000 attributes!
Solution:
Actually, there is no solution: P ~~~ This is to avoid this situation when programming ~~
Note:
1. The above materials are from the official Microsoft MSDN site. link address:
Http://msdn.microsoft.com/librar... e_leak_patterns.asp
You can see the detailed descriptions in the above address, including examples and legends. I am not very familiar with English. If I have any mistakes or need to be supplemented, please point out.
2. For the first article, in fact, element. onclick = funcRef is included, because it is also a reference to the object. It should be released when the page is onunload.
3. For the third article, in the English description of MSDN, it seems that even if detachEvent is called, the memory cannot be released, because at attachEvent, the memory "LEAK" has been generated, however, the situation will be better after detachEvent. I don't know if this is the case. Please be able to point it out in good English.
4. In actual programming, the actual impact of these memory problems is not great, especially when used by the customer, the customer will never be aware of this, however, these problems have always been a heart disease for programmers-do they always feel uncomfortable with such bugs? It is best to solve the problem. As a matter of fact, in top JS source code sites such as webfx.eae.net, I can see in their source code that the above solution is used for memory release management.
When studying jsvm, we found that the gc () method is defined in js. lang. System.
System. gc = function ()
{
If (System. isIeBrowser ())
{
CollectGarbage ();
SetTimeout ("CollectGarbage ();", 1 );
}
}
CollectGarbage () is a memory release function unique to ie.