Silverlight memory problem summary (II)-JavaScript Memory leakage

Source: Internet
Author: User

I still have a lot of feelings about the language called javasrip. I have been using this language since I graduated. At the end of the ASP era, this language began to be despised by many developers. Many people thought that Javascript would only pop up alert and only implement some simple functions, the most annoying thing is that when a syntax error occurs, the error pops up continuously until the developer is desperate and the front-end user is suffocated. With the emergence of Ajax, the success of Google Map, and the emergence of various JS frameworks, JavaScript was once again valued by developers. csdn once described it as "Return of the King. Compared. net, I have a lot of javscript experience, and I have been using it since I graduated. I have been using several popular frameworks, ext, jquery, and prototype, the object-oriented understanding of JS is still in place, and the difference between javascrit in IE series browsers and Firefox is also slightly known. Of course, with the emergence of ie9, this difference will become fewer and fewer. I once had an idea to use JavaScript to implement the 23 gof design patterns, but it was later found that some people have done so in the blog Park, and my interest suddenly disappeared.

 The data communication between the two Silverlight instances requires JavaScript as a bridge, because several threads are running in an endless loop, and some data interaction requires JavaScript, resulting in many JavascriptCodeEndless loop calls lead to memory leakage.

The memory release of JavaScript is also automatically recycled. The basic principle is that when no one points to an object, the garbage collector can start to recycle it. The main cause of JavaScript leakage is:Loop reference,Closure. The circular reference principle is actually quite understandable, for example, a = B = C = A. As a result, all objects A, B, and C cannot be released. Do not underestimate this column, when the code is large, the relationship between the three is hard to be found. As for the closure and scope chain principle, there are many introductions on the Internet.ArticleI have never learned this course because of my major.Compilation principlesSo reading these articles is a little difficult, interested can read (http://kevinpeng.javaeye.com/blog/682653 ). Closure leakage is often causedJavascript objects and Dom are referenced by each other, and some events have not been subscribe to these subscriptions.

Memory leakage caused by DOM reference:

 

 

       Function Leakmemory () // This function can cause cross-page leaks  
{
VaR Hostelement = Document. getelementbyid ("hostelement ");
// Do it a lot, look at Task Manager for memory response
For (I = 0; I <5000; I ++)
{
VaR Parentdiv = Document. createelement ("<Div onclick = 'foo () '> ");
VaR Childdiv = Document. createelement ("<Div onclick = 'foo () '> ");
// This will leak a temporary object
Parentdiv. appendchild (childdiv );
Hostelement. appendchild (parentdiv );
Hostelement. removechild (parentdiv );
Parentdiv. removechild (childdiv );
Parentdiv = Null ;
Childdiv = Null ;
}
Hostelement =Null ;
}
Function Cleanmemory () // This function can not cause cross-page leaks
{
VaR Hostelement = Document. getelementbyid ("hostelement ");
// Do it a lot, look at Task Manager for memory response
For (I = 0; I <5000; I ++)
{
VaR Parentdiv = Document. createelement ("<Div onclick = 'foo () '> ");
VaR Childdiv = Document. createelement ("<Div onclick = 'foo () '> ");
// Changing the order is important, this won't leak
Hostelement. appendchild (parentdiv );
Parentdiv. appendchild (childdiv );
Hostelement. removechild (parentdiv );
Parentdiv. removechild (childdiv );
Parentdiv = Null ;
Childdiv = Null ;
}
Hostelement = Null ;
}
</SCRIPT>
</Head>
<Body>
<Button onclick = "leakmemory ()"> memory leaking insert </button>
<Button onclick = "cleanmemory ()"> clean insert </button>
<Div id = "hostelement"> </div>
</Body>
</Html>

Memory leakage caused by subscription failure:

Function Leakmaybe (){
VaR Elm = Document. createelement ("Div ");
Elm. onclick = Function (){
Return 2 + 2;
}
}
For ( VaR I = 0; I 10000; I ++ ){
Leakmaybe ();
} // Leakmaybe is continuously called in the above loop. Every call will subscribe to an onclick event. If it is an endless loop, leakage will occur.


Many of the memory leaks of Javascript in the IE series are the bugs of IE browsers, but Microsoft does not recognize them,Eric lippert is a member of the IE engine development team. The following is his original saying: Don't use closures unless you really need closure semantics. in most cases, non-nested functions are the right way to go. he pushed the bug to developers and thought it was caused by improper use by developers. Silverlight memory cannot be released, and Microsoft does not recognize it much. This will be mentioned later.

I have read a lot of articles about JavaScript Memory leakage, but I am not very familiar with debugging in my project. The only debugging is setTimeout and setinterval, I checked on the Internet and many people said that these two methods may cause memory leakage in the counter, but I did a test on my own machine and it would not leak, as if IE6 would, however, IE8 is definitely not. I use IE8 on my own. In fact, setTimeout and setinterval only create an endless environment, and actual memory leakage should not be related to them. The memory leakage was found due to the following statement:

Eval ("Var list =" + a), this line of code is mainly to parse a string into a JavaScript Object. According to the old bird outside China, a temporary object will be generated during parsing, this temporary object cannot be released, resulting in leakage. The leakage may be around 14 m a night. Another leak is heartbeat verification (permission verification). An Ajax request is initiated every time a jump occurs. I guess the Ajax object is generated every time, but it cannot be released, the leak caused by this location was 36 m a night. Finally, let's give a tip. If the page is caused by JavaScript leaks, the memory will be released as long as the memory is minimized. This is a personal tips to measure JS leaks, of course, there are still a lot of facts to be verified.

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.