Memory release for JS programming in IE

Source: Internet
Author: User

In JS programming under IE, the following programming methods will cause the failure to release the memory even if IE is disabled. The following categories are provided:

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.