JavaScript Memory leakage (1)

Source: Internet
Author: User

In the past, Web developers did not focus much on memory leaks. At that time, the relationship between pages was relatively simple, and different connection addresses were mainly used to navigate in the same site. This design method is very helpful for the browser to release resources. Even if a Web page is running with a resource leak, its impact is very limited and is often ignored.

BKJIA: Seven JavaScript skills that should have been known

Today, people have higher requirements for Web applications. A page may not jump to a URL for several hours and dynamically update the page content through the Web service. Complex Event Association design, Object-based JavaScript, and extensive use of DHTML technology make the code reach its limits. In this case and change, it is very urgent to find out the memory leakage method, especially in the past, these problems have been blocked by the traditional page navigation method.

The good thing is that when you specify what you want to find, the memory leakage method is relatively easy to determine. Most of the leaks you may encounter are known, and you only need a small amount of extra work to bring you benefits. Although a small number of small leaks still occur on some pages, the main problems are still easy to solve.

Leakage Method

In the following content, we will discuss the memory leakage methods and provide examples for each method. An important example is Closure technology in JavaScript, and another example is to use Closures in event execution. When you are familiar with this example, you can find and modify most of your existing Memory leakage problems, but other Closure-related problems may be ignored.

Now let's take a look at the following methods:

1. The object instances generated by the COM component of the IE browser and the object instances generated by the web script engine will reference each other, causing memory leakage. This is also the most common and major Web page leakage method;

2. Internal function reference (Closures)-Closures can be seen as a special form of loop applications that cause a large number of problems. Closures calls are easily discovered because of the specified keywords and syntax structure;

3. Cross-Page Leaks (Cross-Page Leaks)-in fact, Cross-Page leakage is a small leak, which is usually caused by the internal object thin meter during your browsing process. Next we will discuss the DOM insertion sequence. In that example, you will find that you only need to modify a small amount of code, so that we can avoid the impact of object thinning on Object Construction;

4. It seems that the leakage (Pseudo-Leaks) is not a real leak, but if you do not know it, you may be extremely depressed when your available memory resources become fewer and fewer. To demonstrate this problem, we will overwrite the content in the Script element to cause a large amount of memory leakage ".

Loop reference

Loop reference is basically the start of all leaks. Generally, the script engine processes cyclic references through the garbage collector (GC), but some unknown factors may prevent the release of resources from the environment. For IE, the status of some DOM object instances is unknown to the script. The following are their basic principles:


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.