Simple three steps, the memory leak _yui. Ext Related

Source: Internet
Author: User
Tags mail example
Original address:http://www.jackslocum.com/blog/2006/10/02/3-easy-steps-to-avoid-javascript-memory-leaks/
You may not yet know that most of the JS sites you browse will cause a memory leak. It sounds a bit exaggerated, but it is true, I will lie to you?
Leak monitor
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 will pop up a window to display the details, and can tell you are the object or function caused by the leak.
When I left http://script.aculo.us/.
That should I was the most common leak prototype.js, line 74:3
The Dojo Mail example page
A serious leak pops up two windows (note the scroll bar!).
Word Press
This is even more outrageous. Every time I finish writing press, I'll be there!

Visit a generic site, and you'll find that most sites will have such problems. Indeed, it would be shocking if some sites did not undergo such a test-the appropriate modifications would ensure that the problem did not occur.
Although the site (Jack ' Sblog, example) is all JavaScript, you don't have to worry about any risk of leakage.


The following simple three steps:
1. When you are finished, set your XMLHttpRequest onreadystatechange handlers to NULL,
I use YAHOO.util.Connect for xhr links because it uses polling mechanism instead of readstate, so I don't have to manually set NULL. I recommend that you use YAHOO.util.Connect (or YAHOO.ext.UpdateManager on top of it), if allowed.
2. Clears all DOM event handlers object in the Unload event if they have an opportunity to reference (refence).
If you can make the library do it, don't do it in your own way! I have used YAHOO.util.Event to deal with the mechanism of event events. Other library (prototype, dojo, etc) also have their own corresponding mechanism to deal with. --although I'm not sure how efficient they are to get there. If you take a closer look at the screenshot above, you will find that the leak code above will be associated with the library (with facts proving the source of the accident, Frank Note)
3. In addition to some basic types of data primitive value (String, number), do not put anything else in the DOM expando or property. Of course, you can guarantee that it will be cleared in time, which is an exception.
This is the most important golden rule. You'll find it handy to put something in the Dom expando, and then use $ () to get it, but don't do it. Really, I know what you think, now I have wired to think of paranoia. Yes, a lot of occasions will be the JS object to the DOM expando, and nothing happened, but there will be a lot of .... This situation is not easy to check out Ah! (For example: closure closures). So to avoid any possible occurrence, I will only follow this pattern.
Summary

It is not too difficult to solve the problem. It does not require any skill or experience. As long as you pay attention to the above points, a novice novice also know how to avoid leaks.
Please come to some big websites (including the new Yahoo Mail!?!? , take the time, do your homework, and let me (or someone else) browse your pages without being compromised by memory leaks.

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.