Original address:/blog/2006/10/02/3-easy-steps-to-avoid-javascript-memory-leaks/
You may not know that most of the js websites you browse will cause memory leaks. It sounds a bit exaggerated, but this is the truth. Will I lie to you?
Leak Monitor
Leak Monitor
Leak Monitor
This is a convenient FireFox extension. When you leave that page, it will point to a JavaScript object. If there is a leak, a window will pop up to display the details, and it can tell you that the leak is caused by the object or function.
When leaving/
This should be the most common leak for me, line 74:3
When leaving/
This should be the most common leak for me, line 74:3
The Dojo Mail Example Page
Serious leaks, two windows pop up at once (note the scroll bar!).
Serious leaks, two windows pop up at once (note the scroll bar!).
Word Press
This is even more outrageous. Every time I finish writing Word press, it will appear!
This is even more outrageous. Every time I finish writing Word press, it will appear!
Visiting general sites, you will find that most sites will have this type of problem. Indeed, if some websites have not undergone such tests, it would be somewhat shocking, -- an appropriate modification would ensure that this problem would not occur.
Although this website (Jack'sBlog, example) is all JavaScript, you don't have to worry about any risk of leakage.
The following three simple steps:
1. When you are done, set your XMLHttpRequest onreadystatechange handlers to null,
I use : for XHR linking because it uses polling mechanism instead of readstate so I don't have to set null manually. I recommend you, if permitted, use (or built on it).
2. Clear all DOM event handlers object in the unload event, if they have a chance to reference (Refence).
If you can do something with Library, don’t do it in your own way! I used all the event mechanisms to handle them. Other libraries (prototype, dojo, etc) also have their own corresponding mechanisms to handle them. --Although I don't know where their execution efficiency goes. If you take a closer look at the screenshot above, you will find that the leaked code above will be associated with these libraries (using facts to prove the source of the accident, Frank note)
3. Except for some basic types of data primitive value (String, Number), do not put anything else in DOM expando or property. Of course, if you can guarantee that it will be cleared in time, it is an exception.
This is the most important golden rule. You will find it convenient to put things in DOM expando, and then use $() to get it, but never do this. Really, I know what you are thinking. Now I have accepted the delusion of wires. That's right, many occasions will put JS objects in DOM expando, and nothing happens, but there will be many. . . . This situation is not easy to detect! (For example: closures). So 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 skill or experience. As long as you pay attention to the above points, a novice also knows how to avoid leakage.
Please ask some big websites (including the new Yahoo Mail!?!?) to take some time and do your best to let me (or others) browse your page without suffering from memory leaks.