Three simple steps to implement Memory leakage

Source: Internet
Author: User
Tags mail example

Original address: swap Memory leakage. It sounds a little exaggerated, but it's a fact. Will I lie to you?
Leak Monitor this is a convenient FireFox extension. When you leave the page, it will point to the JavsScript object. If there is a Leak, a window will pop up showing details, it also tells you the leakage caused by the object or function.
When you leave http://script.aculo.us/
This should be my most common leak prototype. js, line 74: 3 The Dojo Mail example Page
For serious leaks, two windows are displayed at once (note the scroll bar !).
Word Press
This is even more outrageous. Every time I finish writing Word press, it will appear!

When you access a common site, you will find that most sites have such problems. Indeed, if some websites do not undergo such tests, they will be somewhat shocked. A proper modification will ensure that such problems will not happen.
Although this website (Jack's sBlog and examples) are all JavaScript, you do not have to worry about any risk of leakage.

The following are three simple steps:
1. After you complete, set your XMLHttpRequest onreadystatechange handlers to null,
I use YAHOO. util. Connect for XHR connection, because it uses polling mechanism to replace readstate, so that I do not need to set null manually. I recommend that you use YAHOO. util. Connect (or the YAHOO. ext. UpdateManager built on it) with your permission ).
2. Clear all DOM event handlers objects in the unload event if they have the opportunity to reference (Refence.
Do not use your own methods for what libraries can do! For the event mechanism, I use YAHOO. util. Event to process. Other libraries (prototype, dojo, etc) also have their own mechanisms to process the event. -- Although I don't know where their execution efficiency goes. If you take a closer look at the above, you will find that the leaked code above will be associated with these libraries (the source of the accident proved by facts, Frank note)
3. Apart from some basic data type primitive values (String, Number), do not put anything else in DOM expando or property. Of course, if you can ensure that it is cleared in time, it is an exception.
This is the most important golden rule. Put things in DOM expando, you will find it very convenient, and then get it with $ (), but never do this. Really, I know what you're thinking, and now I already think about it. yes, JS objects are put in DOM expando in many occasions, and there is no situation, but there will also be a lot .... This situation is not easy to check! (For example, closures). To avoid any possible occurrence, I will only follow this rule.

Summary

It is not too difficult to solve this problem. It does not require any skills or experience. As long as you pay attention to the above points, a newbie novice also knows how to avoid leakage.
Please ask for some big websites (including the new Yahoo Mail !?!?), Spend some time and effort to allow me (or others) to browse your web page without memory leakage.

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.